EDITE: I have more than 100 file
I have multiple files that I want to rotate every time I run a batch.
How to do it in every SUB-FOLDER?
Here's the concept
Please help. Thanks.
#echo off
setlocal enabledelayedexpansion
REM get number of files:
set count=0
for %%a in (*.jpg) do set /A count+=1
REM rename them (decreasing by one):
for /l %%a in (1,1,%count%) do (
set /a new=%%a-1
ECHO ren %%a.jpg !new!.jpg
)
REM rename 0 to max
ECHO ren 0.jpg %count%.jpg
remove the ECHOs when you verified it's what you want.
Related
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 write a simple batch script to use identify (from ImageMagick) to check dimensions of files in this directory and decide its orientation. Later, I'll be adding moving them to seperate folders.
The problem is it can't enter the for loop.
setlocal enabledelayedexpansion
#echo off
for /r %%F in (*.) do (
identify -format "%%w" %%F > temp.txt
set /P temps=<temp.txt
set /A w=%temps%
identify -format "%%h" %%F > temp.txt
set /P temps=<temp.txt
set /A h=%temps%
if /I "%w%" GEQ "%h%" echo Is Landscape
if /I "%w%" LEQ "%h%" echo Is Vertical
pause
)
pause
You can make identify tell you the width and height in a single call for all files in the directory, rather than calling it twice for each and every file:
identify -format "%w %h %f\r\n" *.png
1024 768 a.png
10 10 b.png
So, you can pair that up with your script and do it this way faster and more succinctly:
#ECHO OFF
REM Get dimensions and names of all files in one go
FOR /F "tokens=1-3 delims=+" %%A in ('identify -format "%%w+%%h+%%f\r\n" *.png') DO (
REM %%A is width, %%B is height, %%C is name
ECHO %%A,%%B,%%C
)
I am very new in batch file. what I have now is when i run the .bat it will generate the number of files from the 6 different folders.
#echo off &setlocal
echo Please Enter Cycle Month (MM format)
set /p month=
echo.
echo In the month of %month%,
echo.
set "startfolder1=\\L99016\SBM3276\Backup"
set /a counter1=0
for /r "%startfolder1%" %%i in (*.txt) do set /a counter1+=1
echo SBM3276 have %counter1% files.
echo.
set "startfolder2=\\L99016\SBM3277\Backup"
set /a counter2=0
for /r "%startfolder2%" %%i in (*.txt) do set /a counter2+=1
echo SBM3277 have %counter2% files.
echo.
set "startfolder3=\\L99016\SBM3461\Backup"
set /a counter3=0
for /r "%startfolder3%" %%i in (*.txt) do set /a counter3+=1
echo SBM3461 have %counter3% files.
echo.
set "startfolder4=\\L99016\SBM3462\Backup"
set /a counter4=0
for /r "%startfolder4%" %%i in (*.txt) do set /a counter4+=1
echo SBM3462 have %counter4% files.
echo.
set "startfolder5=\\L99016\SBM3697\Backup"
set /a counter5=0
for /r "%startfolder5%" %%i in (*.txt) do set /a counter5+=1
echo SBM3697 have %counter5% files.
echo.
set "startfolder6=\\L99016\SBM3934\Backup"
set /a counter6=0
for /r "%startfolder6%" %%i in (*.txt) do set /a counter6+=1
echo SBM3934 have %counter6% files.
echo.
pause
the output on the cmd.exe will be:
Please Enter Cycle Month (MM format)
01
In the month of 01,
SBM3276 have 6560 files.
SBM3277 have 6372 files.
SBM3461 have 6228 files.
SBM3462 have 6179 files.
SBM3697 have 6372 files.
SBM3934 have 6372 files.
The question is, how do i filter out to only count the number of files in the specified month which is from the user's input?
Please help.
Thank you very much in advance!
***Note: the code above count all files in the folder with all dates. I only want a specific month. Please help!
#echo off
setlocal enabledelayedexpansion
set /p "inp=Month [MM]: "
FOR %%i in (*) do (
set "dt=%%~ti"
if "!dt:~3,2!"=="%inp%" echo %%i
)
the ~t modifier gives you the date. Extract the month-part to compare.
You can read about modifiers in for /?
As aschipfl notes: "Note that the format of the date given by the ~t modifier depends on the region/locale settings of your system, so the sub-string expansion code might need to be adapted accordingly..."
If you need a solution independent of regional settings, you can adapt the last part of this answer. (although wmic is much slower than just getting a substring)
I have a million old text files that I need to convert the format on. I have been desperately trying to do this myself but I really could use help. I am trying to convert data that looks like this:
text
11111.111
22222.222
33333.333
text2
44444.444
55555.555
66666.666
77777.777
88888.888
99999.999
(each number is on a seperate line and there are some blank lines, but I need them to go into the output file as a place keeper)
Into a .txt file that looks like this:
**I also need to add an increment number at the beginning of each line to number the lines.
1,11111.111,22222.222,33333.333,text
2,44444.444,55555.555,66666.666,text2
3,77777.777,88888.888,99999.999,
the files that I have are in separate folders in a directory and have no file extension but they behave exactly like a standard text file.
I have tried all sorts of stuff but I am just not that well versed in programming. Here is the little bit of code that I havent deleted for the 100th time. gettting frustrated
:REFORMAT
FOR %%F IN (*) DO IF NOT %%~XF==.BAT (
SETLOCAL DISABLEDELAYEDEXPANSION
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO %LINE2%,%LINE3%,%LINE4%,%LINE1%>>"%%F".TXT
PAUSE >NUL
:END
I am using windows I have access to dos6 dos7 winxp 32 and win7 64
the text and text2 are text strings within the file, they are descriptors telling me what the numbers below mean and some of the descriptors are left out. I also need it to process more than the first four lines. Some files have up to 200 lines inside of them. Thank you so much for helping me. thank you so much dbenham here is the final result:
#echo off
setlocal enableDelayedExpansion
for /R %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.CSV"
Using nothing but pure native batch:
#echo off
setlocal enableDelayedExpansion
for %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.txt"
The above will process all files that have no extension in the current folder. If you want to recursively include all sub-folders, then add the /R option to the outer FOR statement.
The whole thing can be done quite simply using my REPL.BAT utility:
#echo off
for %%F in (*.) do (
<"%%F" repl "([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n?" "$2,$3,$4,$1\r\n" mx|findstr /n "^"|repl "^(.*):" "$1," >"%%F.txt"
)
just a few modifications:
set /p doesnt delete a variable if input is empty, so you have to delete it before.
You missed a closing paranthese )
You have to use delayed expansion to use a changed variable inside parantheses
the counter.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
FOR %%F IN (*.) DO IF NOT %%~XF==.BAT (
set "LINE1="
set "LINE2="
set "LINE3="
set "LINE4="
set count+=1
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO !count!,!LINE2!,!LINE3!,!LINE4!,!LINE1!>>"%%F.txt"
)
PAUSE >NUL
:END
I have cracked my head up...
Now I have a folder containing empty subfolders and pictures.
These pictures are randomly named as their default name generated by the camera.
If I want to sort these pictures out into a few batches and then move them into the subfolders
For example:
lets say in total I have 100 pictures and five 5 subfolders.
first 20 pictures into subfolder_1
subsequent 25 pictures into subfolder_2
subsequent 23 pictures into subfolder_3
subsequent 12 pictures into subfolder_4
lastly the remaining 20 pictures into subfolder_5
So, I am thinking of doing it in loops. and since the number of pictures are not constant, and I intend to prompt the user to define number of pictures to be moved each time.
The main thing that I couldn't figure out is that HOW DO I CONTROL THE NUMBER OF LOOPS TO BE DONE?
I know about using GOTO function to break a FOR loop. But I am not sure how to do it in my case.
In fact, I am now still stucked with this concept I currently have, where I tried to use a shorter FOR loop to contain an longer FOR loop like this:
(this is just to try for the first 20 pictures into subfolder_1)
FOR /L %%A in (1,1,20) Do (
FOR %%B in ("%dir_of_folder_which_contains_the_pictures_and_subfolders%\*") Do (
MOVE *.jpg subfolder_1
)
)
These codes don't work. Perhaps it has got to be using GOTO function? Can anyone help? THANKS ALOT..
#echo off
setlocal enableextensions enabledelayedexpansion
set "folder=%cd%"
rem For each of the present subfolders
for /d %%a in ("%folder%\*") do (
rem Count the number of remaining files
set "nFiles=0"
for /f %%b in ('dir "%folder%\*" /a-d /b 2^>nul ^| find /c /v ""') do set "nFiles=%%b"
if !nFiles! lss 1 goto :done
rem Ask the number of files to move
echo(
echo(There are !nFiles! files left. How many to move to %%a ?
set "nFiles=0"
set /p "nFiles="
set /a "nFiles+=0" 2>nul
rem Move the indicated number of files
if !nFiles! gtr 0 for %%c in ("%folder%\*") do if defined nFiles (
echo move "%%~fc" "%%~fa"
set /a "nFiles-=1"
if !nFiles! equ 0 set "nFiles="
)
)
:done
endlocal
exit /b
Not the most efficient code, but this is a basic skeleton to build from. The move command has been prefixed with a echo for testing. If the output to console is correct, remove the echo.
This type of problems may be solved in a better way with the use of data structures, like arrays or lists. For example:
#echo off
setlocal EnableDelayedExpansion
rem Initialize counters
set /A numFolders=0, numFiles=0
rem Save file names in an array
for %%a in (*.*) do (
set /A numFiles+=1
set "file[!numFiles!]=%%a"
)
rem Save folder names in a list
set "list="
for /D %%a in (*) do (
set /A numFolders+=1
set "list=!list! %%a"
)
rem Ask the user for the distribution
:askDistribution
echo There are %numFiles% files and %numFolders% folders
echo Enter the number of files for each folder (must sum %numFiles%)
echo Folders: %list%
set /P "distribution=Files: "
set total=0
for %%a in (%distribution%) do set /A total+=%%a
if %total% neq %numFiles% goto askDistribution
rem Distribute the files
set i=0
for %%n in (%distribution%) do (
rem Get current folder and shift the rest
for /F "tokens=1*" %%a in ("!list!") do (
set folder=%%a
set list=%%b
)
rem Move the files
for /L %%i in (1,1,%%n) do (
set /A i+=1
for /F %%i in ("!i!") do ECHO move "!file[%%i]!" !folder!
)
)
For further details, see: Arrays, linked lists and other data structures in cmd.exe (batch) script