Moving all files from MANY folders up one level - command line - command-line

I have tens of thousands of *.wav files spread out across hundreds of folders. I need help to get command line to move all files up one level. The directory structure for all these files are identical, but the names of the folders vary slightly:
Z:\Audio\Level*\story*\VOCAB\*.wav
All files are located in the VOCAB folders, and I need to move them to the story* folders:
Z:\Audio\Level*\story*\*.wav
I can do this from command line by running a move command on each individual folder, but is there a way to run it recursively on all files within the entire directory? Can I use a wildcard in the location path?
Notes:
The * in Level* and story* are numbers 01-24.
I'm on Windows XP Professional.
Thanks for any help you can provide!

try something like:
for /r %F in (*.wav) do move %F %~pF\..
refer to for /? from command prompt as reference (particularly in case I didn't 'code' that quite right...)
I suggest starting it from within \Audio directory.

Found a similar question in another forum (http://www.computerhope.com/forum/index.php/topic,98046.0/all.html).
Modifying some of their code, here's a batch file script that might do the trick (please try on a small subset before unleashing it on everything):
#echo off
set thisdir=%cd%
for /f "delims=" %%A in ('dir /b /ad') do (
cd /d "%%~dpnA"
for /f "delims=" %%B in ('dir /b /ad') do (
echo Level 2 Directory: %%~dpnB
cd /d "%%~dpnB"
for /f "delims=" %%C in ('dir /b /ad') do (
echo Level 3 Directory: %%~dpnC
cd /d "%%~dpnC"
move *.* ..\
cd ..
rd "%%~dpnC"
)
cd ..
)
cd..
)
cd /d "%thisdir%

Related

How to create directory from URL path separator (forward slash) when that is present in FileName coded %2f or %2F

As I mentioned in Q-title, I have setup a sublime settings auto-syncing process to Github Gist of my choice when I have passed correct Access Token.
Now this process syncs any file that is present inside a folder(s), considering the relative paths, as DirName%2FFileName.extension where DirName and/or FileName may contain singular spaces(%20) between words(if >2 words in either).
Now when I download that gist thro' DownloadZIP button, it ofcourse downloads the whole gist with multiple files as single zip, which after download can be extracted into it's folder in local system(Windows 10/11 OS).
So, the question finally: When I have downloaded such zip and extracted into folder that contains one/multiple files that have %2f or %2F in their names, how do I create a directory from those filenames and move those files into those directory thro' a batch file if possible, or a PowerShell command, but runnable from Batch script, and then also rename those files to remove all DirName%2f parts from their names ? I know about mv and md commands but how do I create directory from URL coded character group and do those operations ?
Note: DirName%2f parts could be occurring in multiples like DirName1%2fDirName2%2f... if the file that is being synced from local is inside mainfolder/subfolders... relatively.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "#name1=DirName%%2f"
SET "#name2=DirName1%%2fDirName2%%2f"
SET "#name3=DirName1%%2FDirName2%%2F"
SET "#2f=%%2f"
SET "#subs=ABC"
SET #
FOR /f "delims=" %%e IN ("%#2f%") DO (
FOR /f "delims=" %%w IN ("!#name1:%#2f%=%#subs%!") DO ECHO REN "%#name1%" "%%w"
FOR /f "delims=" %%w IN ("!#name2:%#2f%=%#subs%!") DO ECHO REN "%#name2%" "%%w"
FOR /f "delims=" %%w IN ("!#name3:%#2f%=%#subs%!") DO ECHO REN "%#name3%" "%%w"
)
ECHO ===================
FOR %%b IN ("%#name1%" "%#name2%" "%#name3%") DO (
SET "#name=%%~b"
FOR /f "delims=" %%e IN ("%#2f%") DO (
FOR /f "delims=" %%w IN ("!#name:%#2f%=%#subs%!") DO ECHO REN %%b "%%w"
)
)
GOTO :EOF
This demo shows how in batch.
You can try :
[System.Web.HttpUtility]::UrlDecode("DirName%2FFileName.extension")
it gives :
DirName/FileName.extension
Then you can use the CmdLets with noun "Path" to manage your file names.
Get-Command -Noun path
If it exists some / in place of \, Powershel should manage them.
The to work with files and folders is given with CmdLet with noun Item :
Get-Command -Noun item
For example to create a directory :
New-Item -name toto -ItemType Directory

Trying to rename files based on file size

I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:

Rename only a specific part of every filename in a folder

I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)

Batch file to find and run latest file in a dir with only partially known name?

How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd

DOS: Copy one file (oldest) at a time

There is a folder which contains many zip files. I want to copy only oldest zip file on each commmand execution. Please help? Thanks
From How do I write a Windows batch script to copy the newest file from a directory? - but reversing the file order
FOR /F "delims=" %%I IN ('DIR . /B /O:D') DO COPY %%I NewDirectoryPath & EXIT