I have a batch file here:
StackOverflows “code” window doesn’t show anything when I press it so hopefully it doesn’t butcher it:
@echo off
echo Moving files to respective folders.
DIR C:\ArcConverted\adam\shapefiles\ /A:-D /B > C:\ArcConverted\adam\filelist.txt
echo File start > C:\ArcConverted\adam\output.txt
FOR /f %%a IN (C:\ArcConverted\adam\filelist.txt) DO (
FOR /f "tokens=1 delims=_" %%b IN ('%%a') do echo A:%%a B: %%b
IF NOT EXIST C:\ArcConverted\adam\shapefiles\%%~na\ mkdir C:\ArcConverted\adam\shapefiles\%%~na\
echo Moving file %%a into C:\ArcConverted\adam\shapefiles\%%~na\
move C:\ArcConverted\adam\shapefiles\%%a C:\ArcConverted\adam\shapefiles\%%~na\%%b
)
pause
I have files in C:\ArcConverted\adam\shapefiles that are newly forward-converted ArcGIS shapefiles, and their filenames are now:
- thisthing_arc.adf
- thisthing_tic.adf
- thisthing_shp.shp
- thatthing_shp.shp
… and so on.
I have 83 of these different object sets, and each has about 16 files. I am trying to make a batch script (and yes, it has to be batch) that reads all the files in the folder, puts it in a file, then reads the list of files and and creates folders for each of them if you take out everything past the _ (the _ is added to it from the converter program).
My script does not work though. The second for loop opens each file and reads everything and prints out hundreds of thousands of lines. I can’t seem to figure out how to do string delimiting on file names using batch (I don’t care about whats in the file).
Summary:
I have file sets that all belong in the same folder thrown into the same folder. I am trying to progmatically (through batch) get the name of the set from the file name (everything that occurs before the _), make that folder, then move the file into that folder. Any files that also belong in that folder will get moved there too as the loop advances.
Am I heading in the right direction?
Thanks.
You were definitely on a good track, but you have both some syntactical errors as well as some logic errors.
In your 2nd FOR loop you are trying to parse a string, but your code is executing a command instead because your IN clause is enclosed in single quotes. You want double quotes to signify a string.
Your whole logic is off starting with your 2nd FOR loop. The string you want to parse is the name of the file, without extension. So your IN clause should be
IN ("%%~na"). Then after that loop you try to create folders named"%%~na"and move the file to that folder with a new file name%%b. But%%bis no longer defined because it is not in your 2nd loop. Actually you want to create folders named"%%b"and move the file there all within the 2nd loop.Lastly you have some issues with your FOR /F options. The 1st FOR will break the name at the 1st space character, which is not what you want. Of course if you never have spaces in your file names then it is a non-issue. But why take the chance? Also the default EOL option of
;will ignore any file names that begin with;. Again unlikely, but why take the chance. File names cannot contain:, so that is a good character for EOL.I believe the following code is what you were looking for.
However, I would simplify the code. I don’t see a need for a filelist.txt file – you can use a simple FOR loop instead of writing the file and reading it with FOR /F. The code is also made much smaller and simpler if you simply change your directory (pushd) to your shapefiles root.
One last thing is to put a filter on the files you attempt to move. I don’t think you want to create folders using the whole name if the file name does not contain a
"_"character. So my modified code explicitly looks for"_"in the name.