I have a folder with all jpg and this is format 1UYK08HJ_20140403165858071_SYPTE1-PC but i want it rename like this
1UYK08HJ_SYPTE1-PC_20140403165858071.jpg. but when i run this script it always ends up with
.%~nA_1UYK08HJ.jpg.jpg.jpg
this is my batch script..
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
for /f "tokens=1* eol=_ delims=_" %%A in ("%%F") do ren "%%F" "%%~nA_%%B%%~xF"
)
popd
This should do it:
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "tokens=1-4 delims=_." %%A in ('dir /b /a-d *_*.jpg') do (
Echo ren %%A_%%B_%%C.%%D %%A_%%C_%%B.%%D
)
popd
Remove the echo when you see correct output.
In the (Window's) folder's command line(cmd) run
ren *.* *.jpg
Related
Hi I am trying to create a Batch script to delete the log files in an application if the file size is more, here is my code. I am getting Syntax error after second "pushd"
#echo off
pushd "C:\Program Files\temp\Logs"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do #del "%%F"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do #del "%%F"
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set maxbytesize = 10
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% GTR %maxbytesize% (
del "%%A"
)
for /f pocesses the content of a file - not what you want here. Use a plain for:
for %%A in ("%file%") do set size=%%~zA
Copy this code exactly and try please.
#echo off
setlocal enabledelayedexpansion
pushd "C:\Program Files\temp\Logs"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do #del "%%F"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do #del "%%F"
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set "maxbytesize=10"
for %%A in ("%file%") do (
set size=%%~zA
if !size! GTR %maxbytesize% del "%%A"
)
How to save a log .txt file of all deleted files with this code?
Also I need to change code to search for all backup folders in root directory subfolders.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
endlocal
This code was written by Mofi and posted as answer on How to remove oldest folder(s) of a group of folders in a folder with several folder groups? He posted also an enhanced version logging the files before deletion, but it was not working for me properly.
I tried with the followed command >> but log.txt file has not been generated.
The attempt to use >> was not shown in the question. How about something like:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
)
endlocal
I have the following batch file:
#echo off
for /f "delims=" %%F in (
'dir /b /a-d [*]*'
) do for /f "tokens=1* delims=]" %%A in (
"%%F"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%F" "%%C"
I want launch it in the root directory and have it go through all directories and subdirectories performing the actions.
I tried adding /D and /r to the 'for' lines, but it doesn't appear to be working.
Do I need add something like...
for /D /r do
under the #echo off ?
Use either dir or for to get all the files, don't mix it up.
When using dir /S for recursive enumeration, regard that full paths are output rather than pure file names only.
This should do it:
#echo off
for /f "delims=" %%F in (
'dir /s /b /a-d [*]*'
) do for /f "tokens=2* delims=]" %%B in (
"%%~nxF"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%~F" "%%C"
So I just changed the following in your original code:
added /s to dir (returns full paths then);
improved second for options (you never used the first token %%A, so why extract it then?);
replaced set %%F of second for by %%~nxF to just parse the file name (type for /? for details concerning substitution modifiers such as ~n, ~x);
replaced source argument "%%F" of ren command by "%%~F" to not fall into double-double-quote problems (the ~ modifier removes potential double-quotes);
You are using "dir" for the enumeration of files, so add "/s" to the DIR command.
I might refactor what you have like this to make it easier to manage.
This also does recursion.
call :TOP .
goto :EOF
:TOP
setlocal
cd "%~f1"
for /f "delims=" %%F in ('dir /b /a-d [*]*') do call :SubRoutine "%%F"
for /D %%x in (*) do call :TOP "%%x" || (echo FAILED2 "%%x" && exit /b 2)
goto :EOF
:SubRoutine
for /f "tokens=1* delims=]" %%A in ("%~1") do call :SubRoutine2 "%~1" "%%A" "%%B"
goto :EOF
:SubRoutine2
for /f "tokens=*" %%C in ("%~3") do ren "%~1" "%%C"
goto :EOF
I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough
I'm trying to write a batch file that will be run on a parent directory and will loop through subdirectories removing all but the newest 3 files from each subdirectory. What I have now recurses through the subdirectories but only skips the 3 newest files that it comes across, not the three newest files in each subdir. I think I need another loop in the code, but need help with where and what it should be. Help!
What I have so far - just ECHOing the output for now as a test.
#echo off
pushd "%~1"
for /f "skip=3 delims=" %%F in (
'dir /s /a-d /o-d /b') do ECHO del "%%F" /f
popd
You might try this:
#echo off
pushd "%~1"
for /D %%i in (*) do (
pushd "%%~i"
for /f "skip=3 delims=" %%F in (
'dir /a-d /o-d /b') do ECHO del /f "%%~F"
popd
)
popd