Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an existing batch file that attempts to move files from 2 directories to 2 different locations. At the moment these are called as 2 separate move commands. I am wanting to simply the code further and was wondering if there was a way to do so

@echo off
for %%a in (C:\Test\*.*) do if "%%~xa" == "" move /Y "C:\Test\%%~na%%~xa" "D:\Done" 
for %%i in (C:\Sample\*.*) do if "%%~xi" == "" move /Y "C:\Sample\%%~ni%%~xi" "D:\Done"

The other scenario is when the destination paths are different as follows

@echo off
for %%a in (C:\Test\*.*) do if "%%~xa" == "" move /Y "C:\Test\%%~na%%~xa" "D:\Done" 
for %%i in (C:\Sample\*.*) do if "%%~xi" == "" move /Y "C:\Sample\%%~ni%%~xi" "E:\Done"
share|improve this question
I am not convinced that shorter==simpler but you can use for %%a in (c:\Test\*.* C:\Sample\*.*) do and then extract the correct directory during the mv. – Paul Aug 28 '12 at 3:07
@Paul - Been a while. Hope all is well. I currently have 5 batch files that do similar tasks hence wanted to simplify it further. For example rather than hardcoding the path, like to have variables I can reference. How would that work if they have to be moved to 2 separate directories? – PeanutsMonkey Aug 28 '12 at 3:14
Your question shows them going to the same directory - can you edit and add some more scenarios? – Paul Aug 28 '12 at 4:19
@Paul - I have updated the question. – PeanutsMonkey Aug 28 '12 at 5:17
I agree with paul. You gain nothing by making the code shorter. You only make troubleshooting more difficult. – Keltari Aug 28 '12 at 5:31
up vote 0 down vote accepted

Your existing command:

for %%a in (C:\Test\*.*) do if "%%~xa" == "" move /Y "C:\Test\%%~na%%~xa" "D:\Done" 

Says "if the file has no extension then move it to d:\Done" - it isn't clear why you are doing the name and extension tests in the move. It looks like this would be just as effective:

for %%a in (C:\Test\*.*) do if "%%~xa" == "" move /Y "%%a" "D:\Done" 

In which case, this would do both directories in the same line:

for %%a in (C:\Test\*.* C:\Sample\*.*) do if "%%~xa" == "" move /Y "%%a" "D:\Done" 

As for picking different destination directories, it would depend entirely what the criteria was.

share|improve this answer
Sorry I misread your answer. The criteria is based on the source destination. – PeanutsMonkey Aug 28 '12 at 6:48
@PeanutsMonkey Your only option would be to do an 'if' inside the loop and pick a different destination based on the source folder. It would be more efficient and readable (simpler) to two this as two separate loops. The string manipulation would be pretty unreadable, and so doesn't meet the question requirement of simplification. – Paul Aug 28 '12 at 7:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .