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.
Related
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"
I need a batch file that when run will look into a specific folder in Program Files (86) and copy and paste that file into another folder. Now I already know the code thanks to this website.
Here is my dilemma: the file changes from computer to computer. I know the first 4 letters are xxxx then followed by an unknown amount of numbers.
Can I make a .bat file that could look for the beginning of a file name?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\abcd*" '
) DO (
SET "name=%%~na"
SET "name=9!name:~4!"
FOR /L %%z IN (0,1,9) DO SET "name=!name:%%z=!"
IF NOT DEFINED name ECHO(COPY "%sourcedir%\%%a" "%destdir%\"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
after setting delayedexpansion mode, perform a directory scan of the source directory, in /b basic mode /a-d without directories and assign each filename found to %%a.
set name fom the name part only of %%a, then remove the first 4 characters and include a 9 into the modified name.
Then remove all 0 to 9 from the result, and if name becomes empty, do the copy. Note that the 9 is added so that the string name remains non-empty until the very last iteration of the for /L loop.
#echo off
pushd "c:\program files (x86)"
FOR /f "delims=" %%a IN ('dir /b /a-d "xxxx*"^|findstr /i "\\xxxx[0-9][0-9]*\."') DO (
ECHO copy "%%a" "c:\another folder\"
)
popd
(NOTE: the copy command is disabled by an ECHO. Remove the ECHO, if the output looks correct.)
filter the output of the dir command to find:
- \\ a literal \ followed by
- xxxx the known prefix followed by
- [0-9] at least one number followed by
- [0-9]* zero or more numbers followed by
- \. a literal .
Pro's: no delayed expansion needed, should be (slightly) faster than Magoo's solution
To look also into subfolders, add /s to the dir command.
I need to do two things...
Rename all text files within a directory. Each file is named with a word and a number with a space between them. I want to remove the number from the file name.
For example....
House 21.txt -> House.txt
Each of the above files have the same content as their name. They each have one word and one number with a space between them. I need to be able to take the word and the number and save them as separate variables.
For example:
Car 2 -> Set var1=Car & var2=2
I just do not know the correct way to strip the appropriate info from the files when the content does not have the same amount of characters. If all I had to do was remove the last 5 characters I could do something like this:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.TXT) do (
set oldName=%%a
set newName=!oldName:~0,-5!
echo !newName!>!newName!.txt
)
But each file is a different length and only removing 5 characters would not work for those that need 6 or more. Can anyone point me in the right direction?
A way to do that :
#echo off
setlocal enabledelayedexpansion
for %%a in (*.txt) do call:treat "%%~na"
exit/b
:treat
set count=1
for %%b in (%~1) do (
set $var!count!=%%b
set /a count+=1)
echo !$var1! - !$var2!
echo ren "%~1%.txt" !$var1!.txt
pause
This will create in every iteration $var1 and $var2 you can then rename %1 as !$var1!.
Just remove the echo if the output is correct for you.
You have 2 unrelated questions.
Part 1 - Rename the files
If all .txt files within the directory that match "* *.txt" are to be renamed, then you can do the following:
:: Preserve any existing files that have no extension
ren *. *.preserve
:: Strip off everything from the space onward
ren "* *.txt" "* "
:: Restore the .txt extension
ren *. *.txt
:: Restore the files with no extension
ren *.preserve *.
This solution has 4 steps, but it operates in bulk, so it can rename a great many files very quickly.
See How does the Windows RENAME command interpret wildcards? for help on why the above works
The above will also rename files like "word1 word2 45.txt", or "word1 word2.txt". You could use the following script to be more specific.
#echo off
for /f "delims=" %%F in (
'dir /b /a-d "* *.txt"^|findstr /ric:"^[^ ][^ ]* [0-9][0-9]*\.txt$"
) do for /f %%A in ("%%F") do ren "%%F" "%%A.txt"
This 2nd solution is very precice, but it must iterate each file, so it will be slower if there are a great number of files.
Part 2 - Capture the content of each line in two variables
#echo off
for /f "usebackq tokens=1,2" %%A in ("house 21.txt") do (
echo the word = %%A
echo the number = %%B
)
Once you have the line parsed into FOR variables %%A and %%B, you can do what you want with them. You could set an environment variable..., whatever.
I have a bunch of text files that I want appending to each other, where the format is like this:
April 14, 2014
00:01:14 0.0952 >100 0.0794 >100
The date is on the 49th line, and I need all lines following, so I'm currently using the following code:
#echo off
for %%a in (*.txt) do more +48 %%a >>##Appended.txt
This works, however in the original text files there are tabs between the numbers, and when I use this command these are replaces by spaces. When I paste the appended file into excel it doesn't automatically split the numbers into correct cells, as it did in the original files.
Thanks,
Chris
Your problem is more- it will convert tabs to spaces
This will get only line 49 of every txtfile (if there is one)
#echo off
for /f %%f in ('dir /b *.txt') do call :getline %%f
exit /b
:getline
for /f "tokens=* skip=48 eol=" %%i in (%1) do (
echo %%i>>##Appended.txt
goto :eof
)
goto :eof
I want to move files according to folder names.
1.Some folder names have been written in b2.txt. In my b2.txt, every line contains one or two or three words, connected by space or "-". Like this:
transfer print
anti-foamer
insect
fibre reinforced plastic
2.My files are in "E:\JP-XIN\".
3.In E disk, there must exist one (only one) folder name consisting of one line in b2.txt.
My question is how to set every line in b2.txt exactly as variable.
In the following code, "%%k" is the file name gotten by searching, "%%l" is the path to the folder gotten by searching. The code did not work correctly.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in (b2.txt) do (
set VAR=%%a
for /f "delims=" %%k in ('dir /s/b/a-d E:\JP-XIN\*.pdf E:\JP-XIN\*.txt ^| findstr /i /c:"!VAR!"') do (
for /f "delims=" %%l in ('dir /s/b/a:d-h E:\ ^| findstr /i /c:"!VAR!"') do (
if not "%%l"=="" move "%%k" "%%~fsl"
)))
pause
I had some time to spent so I worked overtime in how to solve what I think your problem is.
If your requirements are these:
The b2.txt file contains several folder names, with possible spaces.
In E:\ there is one folder that is contained in b2.txt.
In E:\JP-XIN\ there are several *.pdf and *.txt files.
and you want to:
Locate the folder that is contained in b2.txt, and
Move to it the *.pdf and *.txt files that have the same name of the folder
then the Batch file below solve your problem:
#echo off
for /f "delims=" %%a in (b2.txt) do (
if exist "E:\%%a" (
move "E:\JP-XIN\%%a.pdf" "E:\%%a"
move "E:\JP-XIN\%%a.txt" "E:\%%a"
)
)
If the .pdf and .txt are the only files with that name, then the two move commands may be joined in just one:
move "E:\JP-XIN\%%a.*" "E:\%%a"
If this is not your problem then, please, tell us what your problem is!
You need to use tokens=* to return the entire line as a single variable.
for /f "tokens=* delims=" %%a in (b2.txt) do (
You can also write things like tokens=1,2* which makes the first variable token 1, the second variable token 2, and the third variable the rest of the line. So the text:
Several words on a line.
Would split to:
%%a = Several
%%b = words
%%c = on a line.