say I have a set of files, batch script, and subdirectories "Numbers" and "NoNumbers" in a "Files" directory in Windows, like so:
<Files>
<Numbers>
<NoNumbers>
updatefiles.bat
Alpha.txt
Alpha #306.txt
Beta.txt
Gamma.txt
Gamma #402.txt
Epsilon.txt
Epsilon #862.txt
and I want to move all of the files containing #XXX into a subdirectory "Numbers" and all of the ones that do not contain #XXX into a subdirectory "NoNumbers"
After the move, I want to remove the last 4 characters of the files in the "Numbers" directory (eg. Alpha #306.txt renamed to Alpha.txt).
I also want to do all of this using relative paths (e.g. the "Files" directory may be anywhere on any Windows drive).
The end result would look like this:
<Files>
<Numbers>
Alpha.json
Beta.json
Gamma.json
Epsilon.json
<NoNumbers>
Alpha.json
Beta.json
Gamma.json
Epsilon.json
What would the easiest way to do this be with the batch script updatefiles.bat?
Thanks!
EDIT: Here are my initial attempts (tried to do it in pieces first):
Attempting to move the files containing # to subdirectory "Numbers"
ren *.txt *.json
for /f "eol=: delims=" %%F in ('dir /b^|find "#"') do move /Y "%%F" "Numbers"
Attempting to cut off the last 4 characters and .txt extension:
for %%i in ("*.txt") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off last 9 chars
ren "%fname%" "%fname:~0,-9%.json"
goto :eof
Now I need to combine those into one .bat that uses relative paths
EDIT 2: Here's what I've come up with as the combined script:
setlocal
ren *.txt *.json
for /f "eol=: delims=" %%F in ('dir /b^|find "#"') do move /Y "%%F" "Numbers"
for /f "eol=: delims=" %%G in ('dir /b^|find ".json"') do move /Y "%%G" "NoNumbers"
CD /D %~dp0\Numbers
for %%i in ("*.json") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off last 10 chars, then appends .json
ren "%fname%" "%fname:~0,-10%.json"
goto :eof
It renames the .txt to .json, moves all .json files that have "#" in them to the "Numbers" directory, moves everything else to the "NoNumbers" directory, changes to the "Numbers" directory, removes the last 10 characters of each file, and adds back the .json extension. I'm sure there's a more efficient way but it does what I need it to do now.