I'm looking to make a batch file that will remove specific prefixes from files within a folder.
eg. Rename "1 File1.txt" and "1 File2.txt" to File1.txt & File2.txt respectively.
The problem is that that the length of the prefix can vary. Here is the code I have:
#echo off
Set /p Prefix = Enter the prefix to eliminate:
rename "%Prefix%*.txt" "//*.txt"
Is there a way to make the amount of slashes (characters to remove) equivalent to the amount of characters entered by the user? Or another way to achieve the same thing?
Edit: Accidentally listed the files as "1 File1.txt and 2 File2.txt" - this was meant to be "1 File1.txt and 1 File2.txt" - all files with the same prefix (in this case, the prefix simply being "1 "
If you want to remove the prefix automatically, using your example files 1 file1.txt 2 file2txt:
#echo off
set /p "input=Enter Prefix to search: "
for /f %%a in ('dir /b /a-d %input%*.txt') do (
for /f "tokens=1,*" %%i in (%%a) do if not "%%i"=="" if not "%%j"=="" echo move "%%a" "%%j"
)
It will prompt the user for a prefix to search, i.e 1 and then do a search for anything containing 1*.txt and rename it without the first delimeter.
So even if the user adds 255 as prefix, it will search for anything 255*.txt (Note there are no spaces before or after my = in my set command.
This example will simply echo the move command and not actually move the files, if you want to perform the actual task, remove the echo command from the last for loop.
Keep in mind, if a file is called 1 and file1.txt it will rename the file to and file1.txt because of tokens=1,* Also, we are only running this for files containing whitespace as per your examples, not _ - etc, but can be changed if need be.
Lastly, you can also automatically rename all files, without having a user prompted. i.e
#echo off
for /f %%a in ('dir /b /a-d *.txt') do (
for /f "tokens=1,*" %%i in (%%a) do if not "%%i"=="" if not "%%j"=="" echo move "%%a" "%%j"
)
The answer provided by Mofi in the comments is exactly what I'm looking for, thank you!
The common delimeter is a space, so the code works great.
for /F "eol=| delims=" %%A in ('dir "* *.txt" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1*" %%B in ("%%A") do ren "%%A" "%%C"
Related
I am looking for one solution, which will help me to move files to folders with similar name.
I have filenames like TEST1_2018P2.xlsx, TEST2_2018P2.xslx, etc.
And I have folders with names TEST1_City1, TEST2 City2...
What I need is to move file TEST1_2018P2.xlsx to folder TEST1_City1, TEST2_2018P2.xslx to TEST2 City2 and so on.
How can I do that?
Here's my latest code, which is also not working.
#ECHO OFF
SETLOCAL
SET "sourcedir=my_folder"
SET "destdir=my_folder"
FOR /f "delims=" %%a IN ( 'dir /b /a-d "%sourcedir%\*.xlsx" ' ) DO (
FOR /f "tokens=1delims=" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ( 'dir /b /ad "%destdir%\*%%b*" ' ) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
I'm not sure of your exact task, so this relatively basic example should move any .xlsx file to the first existing directory whose name matches the portion of the filename up to the underscore, plus a space.
Adjust the values on lines 2 and 3 to match your actual directory specs, (without trailing backslashes).
#Echo Off
Set "SourceDir=my_folder"
Set "DestDir=my_folder"
For /F Delims^=^ EOL^= %%A In ('Dir /B/A-D-L "%SourceDir%\*_*.xlsx" 2^>Nul'
) Do Call :Sub "%%A"
GoTo :EOF
:Sub
Set "DirName=%~1"
Set "DirName=%DirName:_="&:"%"
For /F Delims^=^ EOL^= %%A In ('Dir /B/AD-L "%DestDir%\%DirName% *" 2^>Nul'
) Do If Exist "%SourceDir%%~1" Move /Y "%SourceDir%\%~1" "%DestDir%\%%A" 2>Nul
Exit /B
It was not designed to be the most efficient method of performing the task!
Please also note that your existing directory names did not have a clear pattern so this was written for TEST1 City1 TEST2 City2 etc.
If they are all underscores, e.g. TEST1_City1 TEST2_City2 etc. then change "%DestDir%\%DirName% *" on line 11 to "%DestDir%\%DirName%_*".
If the directories can be either of them, and you are sure that no two directories will begin with the string TEST1, TEST2 etc., (which would limit you to only numbers 0..9 in this case), you could probably use "%DestDir%\%DirName%?*" on line 11 as an alternative.
Assuming that actual text of TEST1 doesn't contain any _ characters, you can use:
#echo off
setlocal EnableDelayedExpansion
cd /d "your_folder"
for /F "delims= eol=" %%A IN ('dir /B /A-D "TEST*_2018P2.xlsx"') do (
for /F "tokens=1 delims=_" %%B IN ("%%A") do (
rem Define some important variables:
set "token_1=%%B"
set "num_test=!token_1:~-1!"
set "foldername=!token_1!_City!num_test!"
md "!foldername!\" >nul 2>&1
move "%%~fA" "!foldername!\"
)
)
Let me explain my code:
The first for /F loop is used to find all the files you want (TEST*_2018P2.xlsx) excluding all directories (/A-D) and headers. delims= and eol= options are used: loop through the whole line without skipping lines starting with ;.
The second for /F loop is used to get the first token of the output of the first loop (IN ("%%A")).
The first token is set to token_1 variable and then substract the last number/letter from it setting it to the num_test variable.
A foldername is set because it is used two time, it is really hard to understand this code without setting it in a variable. It is actually set by token_1 variable (TESTn), _City and n (number).
A folder is created with that name. Both STDIN and STDERR are redirected to nul. This happens not to have many processed if exist statement. The current file (%%~fA; full path) is moved to this folder.
Remember to replace "your_folder" with your actual folder!
I'm trying to make a .bat file which iterates through every file in the directory which the .bat file is in, and renames all the files which follow the naming convention of MM_DD_YY*.* to YY_MM_DD*.*.
For example, 05.20.16 MyFile.txt would become 16.05.20 MyFile.txt.
I've managed to iterate through the filenames, but I'm having trouble with making a substring of the beginning 8 characters and comparing them to the format of ##.##.##.
#echo off
setlocal EnableDelayedExpression
for /f %%i in ('dir /b') do (
set fn=%%i
:: Need to compare first 8 chars to ##.##.##
if %fn:~0,8%==??.??.?? echo FollowsFormat
)
Any help is appreciated!
Provided:
the date in the file name is always followed by a space.
You can use delimiters . and spaceto seperate date elements and remaining file name into for variables %%A - %%D and rearrange them in the desired order.
To better distinguish old from new format I suggest to change the delimiter (at least temporarily) to a - and extend the year to 4 digits.
#echo off & setlocal EnableDelayedExpression
for /f "tokens=1-3* delims=. " %%A in (
'dir /b /A-D "??.??.?? *.txt" ^| findstr "^[01][0-9]\.[0-3][0-9]\.[0-9][0-9].*"'
) do Echo Ren "%%A.%%B.%%C %%D" "20%%C-%%A-%%B %%D"
For security reason the batch only echoes what it would rename
until you remove the echo in front of ren if the output looks OK.
I have some folders which says AA2017-123-TEXT and AA2017-124-TEXTS.
I'm trying to use batch to rename these file to AA2017-123 and AA2017-124 removing the text from the folder name. That is I want only the first 8 characters in a folder name.
I am using windows 7
To do exactly what have been asked for, namely splitting off a certain amount of characters, the following code snippet could be used (extracting the first 10 characters here for example):
for /F "delims=" %%D in ('dir /B /A:D "*"') do (
set "FOLDER=%%D"
setlocal EnableDelayedExpansion
move "!FOLDER!" "!FOLDER:~,10!"
endlocal
)
There are several ways to accomplish your task, IMO the simplest is using the hyphens as delimiters in a for loop.
In the cmd line
for /f "tokens=1,2* delims=-" %a in ('dir /B/ad *-*-*') do #echo ren "%a-%b-%c" "%a-%b"
In a batch
for /f "tokens=1,2* delims=-" %%a in ('dir /B/ad *-*-*') do echo ren "%%a-%%b-%%c" "%%a-%%b"
If the output looks ok remove the echo
I have around 3000 txt file and I want to split each file into two separate txt files based on specific phrase, for example section one and section two.
Each file looks like this :
Section one
Xxxxxxxxx
Xxxxxxxxxx
Xxxxxxxxx
Section two
Xxxxxxxxxc
Xxxxccxccc
Xxxxxxxcxx
I want to have section one and section two in two separate txt files.
Please suggest or advise me any help to perform this task. I heard about batch file but I have no idea how it works.
I'm using windows 10.
Many thanks in advance.
I agree to the previous comments. If you want to know what's going on, you should use your own efforts in reading the help for the commands used here (ie for /?, findstr /?, ...).
Despite the above, give this a try. It may need modifications involving handling paths or whatever your real task may be. Try your own efforts and then come back to show your progress, where are you stuck, and may be find a solution.
This script split txt files where section two is found and changes extension from blabla.txt to blabla.one.txt and blabla.two.txt each of these containing the splitted version of the original file.
The strings Section ... are supposed to be at the beginning of the line.
#echo off
for /F %%f in ('dir /B *.txt') do (
for /F "usebackq tokens=1-3 delims=: " %%1 in (`findstr /B /N "Section" "%%f"`) do (
if /I "%%3" EQU "two" call :split %%f, %%1
)
)
exit/B
:split
Setlocal
set "fileone=%~1" & set "filetwo=%~1" & set/a split=%2
set "fileone=%fileone:.=.one.%"
set "filetwo=%filetwo:.=.two.%"
copy NUL "%fileone%">NUL
copy NUL "%filetwo%">NUL
for /F "tokens=1,* delims=[]" %%a in ('"type "%~1"|find /N /V """') do (
if %%a lss %split% (
echo(%%b>>"%fileone%"
) else (
echo(%%b>>"%filetwo%"
)
)
Endlocal
exit/B
EDIT Did a rework of my batch to get all files and circumvent recursing already splittet files
#Echo off
For /f "Delims=" %%F in ('Dir /B/S File*.txt^|Findstr /v "_"'
) Do Set "Fout=NUL"&Set "Fin=%%~F"&For /f "Tokens=1*Delims=:" %%A in (
'Findstr /N /V §³$ %%F') Do Call:Sub %%B
Goto :Eof
:Sub
If %1. Equ Section. Call :SetFout %Fin% %2
>>"%Fout%" Echo:%*
Goto :Eof
:SetFout
Set "Fout=%~dpn1_%2%~x1"
Type NUL>"%Fout%"
Some explanations
findstr /N is used to number the lines (required otherwise empty
lines would be dropped)
/V §³$ means every line NOT containing these chars, ergo all.
for /f parses the line and cuts of the number and the remainder of the
line is an arg to the call :sub
if the first word is Section Sub :SetFout is called to build a new name Fout from Fin (In file) by appending _secondWord to it (one, two) The new file is set to NUL
All other lines are simply copied to the current Fout
I am using mkvmerge to extract audio from multiple files. When doing that, I want to rename the extracted files so it is easier for me to merge them later.
My command right now is this
for /f "delims=" %%A IN ('dir /b *.mkv') DO mkvextract tracks "%%A" 2:F:\%%~nA.ogg
Which extracts an audio from a file say "x1.mkv" to "x1.ogg"
What I want to do is, rename the audio file (x1.ogg) to the name of another file from another folder.
Kinda like this command (Which I know is wrong, I'm trying to tell what I want to achieve):
for /f "delims=" %%A IN ('dir /b *.mkv') && %%B in ('dir "F:\Show" /b *.mkv') DO mkvextract tracks "%%A" 2:F:\%%~nB.ogg
Which Takes a file from my current directory (%%A) , but the extracted audio will have the name of the corresponding file in another directory (%%B).
Thus,
x1.mkv => y1.ogg
x2.mkv => y2.ogg and so on..
So how do I type the FOR command in which 2 variables have names of files of 2 different directories?
Thanks!
#ECHO OFF
SETLOCAL
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=:" %%A IN (
'dir /b /a-d "*.mkv"^|findstr /n /r "." '
) DO (
FOR /f "tokens=1*delims=:" %%D IN (
'dir /b /a-d "%destdir%\*.mkv"^|findstr /n /r "." '
) DO IF %%A==%%D echo(mkvextract tracks "%%B" 2:F:\%%~nE.ogg
)
GOTO :EOF
You would need to change the setting of destdir to suit your circumstances.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
"filter" any line from the directory list containing any character, and number the line with a preceding #:. This has the effect of simply producing the same list, but numbered with #: preceding.
Tokenise each line into %%A and %%B using the separating : so the number goes to %%A and the filename to %%B.
With each %%A,%%B, do the same trick to the other directory, this time producing %%D,%%E, then if the line numbers match, execute your command with the appropriate selection of elements from the variables.