I wanted to make a batch script that would go into the folder %~dp0\Playlists\%list% and rename each .mp3 file in there to the next number.
For example, the top file would be renamed to "1" and the second would be renamed to "2". I looked everywhere but I couldn't find out why my code wouldn't work.
set playnmbr=0
for /f "usebackq delims=|" %%f in (`dir /b "%~dp0\Playlists\%list%"`) do (
rename %~dp0\Playlists\%list%\%%f %playnmbr%.mp3
set /A playnmbr=%playnmbr%+1
)
You are missing setlocal enabledelayedexpansion (as npocmaka wrote):
setlocal enabledelayedexpansion
set playnmbr=0
for /f "usebackq delims=|" %%f in (`dir /b "%~dp0\Playlists\%list%"`) do (
rename %~dp0\Playlists\%list%\%%f !playnmbr!.mp3
set /A playnmbr=!playnmbr!+1
)
Also, %list% is not defined in your code.
Related
Hi guys I'm new to batch and have a question for my .bat to rename files.
I looked at the following solution and tried to transfer this to my problem:
Renaming file names with a BAT file
So my .bat looks like this one:
setlocal enabledelayedexpansion
set /a count=1
set padded_count=000!count!
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !padded_count!.txt
set /a count+=1
)
And I have a file with random names for .txt data.
E.g.
abc.txt
def.txt
123.txt
456.txt
And I want to change these into:
0001.txt
0002.txt
...
But when I use my .bat its just the first .txt which changes its name.
Can you explain me why? And what should I do to get all of these.
Or is it possible to handle this problem with REN in cmd with something like "ren *.txt ___"
setlocal enabledelayedexpansion
set /a count=10001
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !count:-4!.txt
set /a count+=1
)
where !count:-4! selects the final 4 characters of count.
After your comment on the requirement, This is similar to #Magoo's answer, but I am not limiting it to 4 chars.
#echo off
setlocal enabledelayedexpansion
set count=10000
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
if "!count:~1!" == "9999" set count=100000
set /a count+=1
echo ren "%%a" !count:~1!.txt
)
In this instance, once we get to 9999 we set a new count variable so out files will continue with an additional digit.
ren "file9999.txt" 9999.txt
ren "file10000.txt" 00001.txt
ren "file10001.txt" 00002.txt
...
I want to write a batch script to rename folders in a directory.
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
So if I have 20 folders, 20 names would exist in file to rename each folder.
What I have so far:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
The above script didn't give me the desired result.
Not tested:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
I am having an question
How can i loop through an folder and only show folders and their subfolders without the full path.
example
if i use dir /b /s /ad
i will see e:\Mainfolder\Folder1\Subfolder
And i only whant to see
Folder1\Subfolder
the reason i want it is so i can put it inside an txt field and then with another loop check if the folder/subfolder exist somewhere else if not then it needs to create it.
With kind regards,
Thomas de Vries
#echo off
setlocal enableextensions disabledelayedexpansion
set "startingFolder=%cd%"
:: Determine the length of the starting path to remove
:: it from output
for /d %%a in ("%startingFolder%\"
) do for /f "skip=1 tokens=1 delims=:" %%b in (
'(echo(%%~fa^&echo(^)^|findstr /o "^"'
) do set /a "cutPoint=%%b-3"
:: Recurse folders from starting point and echo the
:: full path without the starting folder
for /r "%startingFolder%" /d %%a in (*) do (
set "line=%%a"
setlocal enabledelayedexpansion
echo(!line:~%cutPoint%!
endlocal
)
This will recurse over the tree structure, starting at the indicated folder (change startingFolder variable to what you need). For each folder found its relative path is echoed to console. Redirect output of batch to generate the required .txt file
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b /s /ad') do (
set "name=%%a"
echo !name:*e:\Mainfolder\=!
)
I have two text files...files.txt containing a list of filenames and dirs.txt containing the list of directories the files need to be copied to.
This is how the files need to be copied:
File 1 ------------------------> Folder 1
File 2 ------------------------> Folder 2
File 3 ------------------------> Folder 3
How do I implement this using batch? Thanks in advance...
Try this:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (files.txt) do (
set /p dir=
echo copy "%%~a" "!dir!"
)<dirs.txt
pause
The above works - Mona can revise or remove the following:
setlocal enabledelayedexpansion
3<dirs.txt(
for /f "delims=" %%a in (files.txt) do (
set /p dir=<&3
copy "%%~a" "!dir!"
)
)
And that should do what you want. Note if dirs.txt has less lines then files.txt, this will fail.
Mona.
Well I managed to figure it out...thanks to this answer from #foxidrive. Here is the code:
#echo off
setlocal enabledelayedexpansion
set /A i=0
for /F "usebackq delims==" %%a in (files.txt) do (
set /A i+=1
call set array1[%%i%%]=%%a
call set n=%%i%%
)
set /A i=0
for /F "usebackq delims==" %%a in (dirs.txt) do (
set /A i+=1
call set array2[%%i%%]=%%a
)
for /L %%i in (1,1,%n%) do call copy "%%array1[%%i]%%" "%%array2[%%i]%%"
It's definitely not the best solution...but it works!
Thanks everyone for your help.
I need to rename all files in a folder with numbering, for example - i got 4 files (test.txt test(1).txt test.doc test(1).doc) and i need to rename them to name1.txt name2.txt name3.doc name4.doc
im currently trying this script but it does not work properly
#ECHO off
for /L %%n in (1,1,10) do rename D:\folder\*.* name%%n.*
end
any advices? thanks
#echo off
pushd c:\someDir
setlocal enableDelayedExpansion
set /a counter=0
for /f "delims=" %%a in ('dir /b /a-d *') do (
set /a counter=counter+1
ren "%%~nxa" "NAME!counter!%%~xa"
)
endlocal