0

I have several files that I want renamed and moved to their respective folders based on their filename. My files are in the format:

village--rural--rodriguez.txt
city--rural--santa-ana.txt
city--urban--san-diego.txt
city--no-data--san-marino.txt

and I want the final output to be:

village (folder) containing rural (subfolder) containing rodriguez.txt

city (folder) containing rural (subfolder) containing santa-ana.txt

city (folder) containing urban (subfolder) containing san-diego.txt

city (folder) containing no-data (subfolder) containing san-marino.txt

I have read other similar questions and tried this code which works for most files. The only problem is that my some of my filenames have single hyphens - in them (like no-data in city--no-data--san-marino.txt), which makes the batch file ignore the processing of such files:

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\Users\Documents"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
 'dir /b /a-d *--*--*.*'
 ) DO if "%%c" neq "" if exist "%%a--%%b--%%c" (  
 MD "%%a" 2>nul
 MD "%%a\%%b" 2>nul
 ren  "%%a--%%b--%%c" "%%c"
 MOVE "%%c" ".\%%a\%%b\" >nul
)
POPD
GOTO :EOF

How can I make the batch file process the filenames, whether with hyphens or not?

JAT86
  • 997
  • 12
  • 24
  • Can't reproduce the issue. It works ok on my PC. – DodgyCodeException Feb 08 '18 at 18:03
  • I suggest you do the move first and the ren after. Just in case there already is a `%%c` file in the source directory. – DodgyCodeException Feb 08 '18 at 18:06
  • @DodgyCodeException: I edited the details. Please check. The problem is that files containing 'no-data' string in the filename are ignored (because of the hyphen). – JAT86 Feb 08 '18 at 18:13
  • 2
    Get the file name into a variable first. So your first `FOR` command will to get the list of file names. As you are parsing the file names use a `SET` command to do string replacement on the double hyphens. Change the double hyphens to another delimiter that isn't in your filenames. You can then pass that changed file name into a `FOR /F` command to split up the file name based on the new delimiter. – Squashman Feb 08 '18 at 18:50
  • 2
    `MD "%%a" 2>nul` is not needed as command __MD__ creates entire directory tree if necessary on command extensions being enabled as by default and needed for this batch code as otherwise __FOR__ would not support option `/F`. And there is no need to first ren the file and then move it. The command __MOVE__ can move a file X to another directory with new name Y. So `MOVE "%%a--%%b--%%c" "%%a\%%b\%%c" >nul` or `MOVE /Y "%%a--%%b--%%c" "%%a\%%b\%%c" >nul` to overwrite an existing file with same name in target directory is enough. Run in a command prompt window `md /?` and `move /?` for help. – Mofi Feb 08 '18 at 21:48

1 Answers1

2

You could use this option if you think it works better for you:

@Echo Off
SetLocal EnableDelayedExpansion
Set "SourceDir=%UserProfile%\Documents"
CD /D "%SourceDir%" 2>Nul || Exit /B
Set "i=0"
For %%A In (*--*--*.*) Do Call :Sub "%%A"
Exit /B

:Sub
Set "_=%~1"
Set "i=1"
Set "_!i!=%_:--="&Set /A i+=1&Set "_!i!=%"
If Not Exist "%_1%\%_2%\" MD "%_1%\%_2%"
Move /Y %1 "%_1%\%_2%\%_3%">Nul
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    How does that work? In particular `Set "_!i!=%_:--="&Set /A i+=1&Set "_!i!=%"` looks complicated. – DodgyCodeException Feb 09 '18 at 09:59
  • 2
    It's not complicated, they only have to copy and paste it! However for more information on how it works see [here](https://www.dostips.com/forum/viewtopic.php?t=6429). – Compo Feb 09 '18 at 11:21