I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"
Related
Want to make a batch file from a folder of 30,000 files from let's say C:\Users\NAME\Desktop, that will create a folder named "1", put 500 files in, no matter the name of the file, and then loop naming the next "2", until all 30,000 are in folders of 500 files each.
Thanks in advance for the help :)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
PUSHD "%sourcedir%"
ECHO sd=!sourcedir!
FOR /f "tokens=1*delims=:" %%b IN (
'dir /b /a-d * png *.doc^|findstr /n "."'
) DO (
SET /a subdir=1+(%%b / 7^)
FOR /f "tokens=2delims==" %%e IN ('set subdir') DO (
ECHO MD "%destdir%\%%e" 2>NUL
ECHO MOVE "%%c" "%destdir%\%%e\"
)
)
popd
GOTO :EOF
Always verify against a test directory before applying to real data.
I used a limit of 7 for testing. Modify as you will
Required md and move commands are merely echoed for verification. remove the echoes to activate after testing.
Found this from Compo, sorry didn't reference it!
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=2"
For %%A In (*.png *.doc) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
I'm trying to write a code that does the following. I have some files in a directory with specific extensions. I have made a vector that contains all of them. Now I will want to rename each file to something else depending on their extension. So for that I'm trying to parce the created vector with a for loop in which I check for each element extension.
For now I won't rename it just echo it on the screen if the file with the .elf extension is found. I wrote this code but I get no echo as in there would be no .elf file in my directory. Please help me correct this. Thanks.
#echo off
setlocal enabledelayedexpansion
cd C:\Users\uidr0938\Desktop\Copy
set path=C:\Users\uidr0938\Desktop\Copy
set /a index=0
for /r %%i in (*) do (
set value[!index!]=%%i
set /a index+=1
)
set /a limit=%index%-2
for /l %%a in (0;1;%limit%) do (
if !value[%%a]! equ *.elf (
echo !value[%%a]!
)
)
endlocal
try with :
....
for /l %%a in (0;1;%limit%) do (
if "!value[%%a]:~-4!" equ ".elf" (
echo !value[%%a]!
)
)
when comparing string you cannot use wildcards.Here you can see some examples about batch substrings
Here is a slightly different way of doing it.
#IF NOT EXIST "%USERPROFILE%\Desktop\Copy\" #EXIT/B
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_path=%USERPROFILE%\Desktop\Copy"
SET "_index=0"
FOR /F "DELIMS=" %%A IN ('WHERE/R "%_path%" * 2^>NUL') DO (SET/A "_index+=1"
SET "_value[!_index!]=%%A")
IF %_index% EQU 0 EXIT/B
FOR /F "TOKENS=1* DELIMS==" %%A IN ('SET _value['
) DO IF /I "%%~xB"==".elf" ECHO %%B
PAUSE
I picked up the code below from another post. I believe it should pick up the current directory folder and include it in the renaming part of the process, however that doesn't seem to work for me.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD
I have a .txt file that will be received into a folder each day named and time stamped. Example as below:
FileNameA_20170418153000.txt
Essentially I'd like to amend the code above to rename the file: filenam0001.txt and continue to update the sequence number (which works perfectly well).
i.e.
filenam0001.txt
filenam0002.txt
filenam0003.txt
Any help would be greatly appreciated.
you need to add the leading zeros manually (add some zeros, then cut the last x characters):
#echo off
setlocal EnableDelayedExpansion
set inc=0
for /l %%a in (1,1,50) do (
set /a inc+=1
set num=00000000!inc!
set num=!num:~-5!
echo !num!
)
I modified your code in order to insert the leading zeros in a simple way...
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=10000
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1_%%~na!inc:~1!%%~xa"
Ren "%%a" "%~n1_%%~na!inc:~1!%%~xa"
)
POPD
So in Windows Explorer, I have these files sorted like this:
I have this script to remove the brackets and one zero, and in case the trailing number is greater than or equal to 10, to remove two zeroes:
cd C:\folder
setlocal enabledelayedexpansion
SET /A COUNT=0
for %%a in (*.jpg) do (
SET /A COUNT+=1
ECHO !COUNT!
set f=%%a
IF !COUNT! GTR 9 (
set f=!f:^00 (=!
) ELSE (
set f=!f:^0 (=!
)
set f=!f:^)=!
ren "%%a" "!f!"
)
pause
However, once I run the code, I get this result:
So the batch file isn't going through the files "intuitively" like Windows Explorer shows them. Is there any way to change this? Or is there a better way to rename these files altogether?
This uses a different approach:
#echo off
cd C:\folder
setlocal enabledelayedexpansion
SET /A COUNT=0, REMOVE=2
for /F "delims=(" %%a in ('dir /B *.jpg') do (
SET /A COUNT+=1
ECHO !COUNT!
set "f=%%a"
IF !COUNT! EQU 10 SET "REMOVE=3"
for /F %%r in ("!REMOVE!") do set "f=!f:~0,-%%r!"
ren "%%a" "!f!!COUNT!.jpg"
)
pause
Here is a method that does not depend on the sort order used by the file system, preserving the numbers as occurring in the original file names.
For each file name (for instance, test_this_01 SELR_Opening_00000 (1).jpg), the portion after the last under-score _ is retrieved (00000 (1)). Then the parentheses and the space are removed and then the length is trimmed to five characters (00001). This string replaces the original one in the file name finally (test_this_01 SELR_Opening_00001.jpg); the file name must not contain the replaced portion (00000 (1)) multiple times (hence file names like this should not occur: test_this_00000 (1) SELR_Opening_00000 (1).jpg):
#echo off
setlocal DisableDelayedExpansion
rem // Define constants here:
set "LOCATION=."
set "PATTERN=*_* (*).jpg"
set /A "DIGITS=5"
pushd "%LOCATION%" || exit /B 1
for /F "usebackq eol=| delims=" %%F in (`
dir /B /A:-D /O:D /T:C "%PATTERN%"
`) do (
set "FILE=%%F"
setlocal EnableDelayedExpansion
set "LAST="
for %%I in ("!FILE:_=","!") do (
set "LAST=%%~nI" & set "FEXT=%%~xI"
set "FNEW=!FILE:%%~I=!"
)
set "LAST=!LAST:(=!" & set "LAST=!LAST:)=!"
set "LAST=!LAST: =!" & set "LAST=!LAST:~-5!"
ECHO ren "!FILE!" "!FNEW!!LAST!!FEXT!"
endlocal
)
popd
endlocal
exit /B
Adapt the directory location and the file search pattern in the top section of the script as you like.
After having tested, remove the upper-case ECHO command in order to actually rename files.
I'm trying to create a batch file that creates folders 1.2.0.1, 1.2.0.2,1.2.0.3, etc.
I fould this code from a previous question and it almost works for my needs:
#echo off
setlocal enableDelayedExpansion
set "baseName=New_Folder"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
so the way I attempted it is to have two variables, one holds the first three digits 2.4.0 and the other holds the forth one.
set version=1.2.0
cd C:\Users\Build
set "lastFolder=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%version%*."^|findstr /xri "%version%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%version%=!"
if !name! gtr !lastFolder! set "lastFolder=!name!"
)
set /a nextFolder=lastFolder+1
md "%version%.%nextFolder%"
but this doesn't work because it's only creating the first folder and not the ones following. Can anyone help me determine the issue? Thanks
This will create folders, if they exist or not.
#echo off
for /L %%a in (1,1,255) do MD "1.2.0.%%a" 2>nul
This should create only the next folder in the series:
#echo off
cd C:\Users\Build
for /L %%a in (1,1,20000000) do if not exist "1.2.0.%%a\" MD "1.2.0.%%a" & goto :done
:done
This is your code that now works - and it does exactly the same as the code above, within reason. 2 million folders seems to be a fair number, even if it takes a few seconds. :)
#echo off
setlocal enabledelayedexpansion
set version=1.2.0.
cd C:\Users\Build
set "lastFolder=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%version%*."^|findstr /xri "%version%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%version%=!"
if !name! gtr !lastFolder! set "lastFolder=!name!"
)
set /a nextFolder=lastFolder+1
md "%version%%nextFolder%"
pause
Missing ! and . is not being correctly handled:
...
'2^>nul dir /b /ad "%version%.*"^|findstr /xri "%version%.[0-9]*"'
...
set "name=!name:*%version%.=!"
...
set /a nextFolder=!lastFolder!+1
...