1

The detail requirement is I need to move files from source folder to Destination folder based on the File names. For example

ABC_TEST1.txt should be moved to Y:\Dest\ABC, EFG_TEST1.txt should be moved to Y:\Dest\EFG, XYZ.txt should be moved to Y:\Dest\Unkonow, GHI_TEST1.txt should be moved to Y:\Dest\Unkown.

I tried the script below.

setlocal EnableDelayedExpansion

for %%F in (*.txt) do ( 
    set FileName=%%F
    echo %%F
    if x%FileName:_=%==x%FileName%(
        for /F "tokens=1* delims=_" %%B in ("%%~nXF") do (
            ren "%%F" "%%C"
            if exist "Y:\Dest\%%B" (move "%%C" "Y:\Dest\%%B")   else (move "%%C" "Y:\Dest\Unknown")
        )
    )else (move "%%C" "Y:\Dest\Unknown")
        
)
endlocal

If I add the if condition the script is not working, without the if condition it's working.

ABC_TEST1.txt should be moved to Y:\Dest\ABC, EFG_TEST1.txt should be moved to Y:\Dest\EFG

It's not moving the XYZ.txt, GHI_TEST1.txt to Y:\Dest\Unkonow.

Compo
  • 36,585
  • 5
  • 27
  • 39
reddy
  • 11
  • 1
  • 1
    You have enabled delayed expansion, but are not using it. Also there should always be a space in front of an opening parenthesis which does not begin a command. ```if x%FileName:_=%==x%FileName%(``` needs to be ```if x!FileName:_=!==x!FileName! (``` and ahould preferably be ```if "!FileName:_=!" == "!FileName!" (```. – Compo Jan 30 '23 at 01:55

0 Answers0