I have a bunch of files (and more coming) that are downloaded from the internet (pdf printed) but they do come with a lot of names that I do not need/want; so I want to have a text file with the things that I want to remove. example
I have a text file with this two lines
Chapter 13. Text and Regular Expressions - Master-PowerShell _ With Dr. Tobias Weltner - PowerShell.com
- PowerShell Scripts, Tips, Forums, and Resources.htm
I want to keep adding lines of text to that file and then call it from another batch file to remove all the lines that are in this file, I got something working that I took from foxidrive here
I could keep adding strings to the rename, but I'm wondering if I could get all the lines to remove from a text file, and I know that there are programs out there (free ones) to do that, but I'd rather use the good old DOS batch.
Thanks
Here is my code
#echo off
if Exist %CD%/temp.txt del %CD%/temp.txt
echo #echo off >%CD%/temp.txt
echo for /f "delims=" %%%%a in ('dir /a:-d /o:n /b') do call :next "%%%%a" >>%CD%/temp.txt
echo GOTO:EOF >>%CD%/temp.txt
echo :next >>%CD%/temp.txt
echo set "newname=%%~nx1" >>%CD%/temp.txt
Echo.>>%CD%/temp.txt
for /f "delims=" %%l in (%CD%\To_remove.txt) Do (
echo set "newname=%%newname:%%l=%%">> %CD%\temp.txt)
Echo.>>%CD%/temp.txt
echo ren %%1 "%%newname%%" >>%CD%/temp.txt
Start Temp.txt
Exit
I did save it as txt and opened at the end of script to see how it looks, it's just a matter of renaming it to .cmd and works.
I had to add comments and edit my post to add it all together but works
Thanks
Related
Hi I have multiple file which are having tie stamp (random).
PMPRO_Outbound_US05_20170927_114630.csv
PMPRO_Outbound_US05_20170928_115430.csv
etc.
I want to rename these files using batch script, could you please help.
PMPRO_Outbound_US05_20170927_114630.csv
to
PMPRO_Outbound_US05_20170927_114630.csv_1
Please help.
using a for loop:
for /F %%i in ('dir /b *.csv') do echo mv %%i %%i_1
Once you are comfortable that it is doing what you intended to do, remove echo to perform the actual rename.
I'm trying to grab the file name from a .LOG file and append it to the beginning of each line in the file, sans extension, with a space between the file name and the line contents. There are many such files in a folder and the script should be performed for each .LOG file in the folder. An example file name looks like this:
20160201.log
I want to add the file name to each of the lines in the file which look like this:
00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
To give a result of this:
20160201 00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
20160201 00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
20160201 00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
I saw this question here but it references pulling a filename from a list of filenames and placing at the beginning of each entry respectively, so that doesn't work for me. I also looked at this question, but since I need to use the file name instead of my current system time that won't work either. I don't have any code to offer...I couldn't use anything conclusive from the questions I found and I'm totally new to batch scripts.
EDIT 1: Magoo's code
Here is the current code I'm trying to run using Magoo's answer (Note the comments are my addition for troubleshooting):
#ECHO Off
SETLOCAL
SET "sourcedir=C:\Users\ckemmann\Desktop\Current"
SET "destdir=C:\Users\ckemmann\Desktop\Current\New"
::echo %sourcedir% <I check what I've assigned to sourcedir.
::echo %destdir% <And to destdir.
::pause
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
::pause <I catch the batch script telling me that the system cannot find the path specified.
GOTO :EOF
As shown in the code, I get a complaint from windows saying that it can't find the file path...even though that's the one I copied from the folder properties.
EDIT 2:
Since I didn't hear back on my edit 1 I went with Arescet's answer. I sadly don't seem to have any way around the slowness of the script. My final code is the same as what he provided in his answer.
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
Decoding...
Take the files matching "*.log" from the specified directory, assign their names to %%f
create a new file %%~nf.out in the destination directory. Read each line from %%f to %%a and echo that line, prefixed by %%~nf and a few spaces.
%%~nf means the name part only of %%f.
The redirector > createas a new file and in the position it's been placed, accepts the output of the echo into that file instead of the normal echo-to-console.
#echo off
for /r "C:\PATH TO WHEREVER\" %%G in (*.log) do (
ren "%%~G" "reWrite.temp"
for /f "tokens=*" %%H in (reWrite.temp) do (
echo %%~nG %%H >> %%~nG.log
)
del "reWrite.temp"
)
pause
This takes all .log files in "C:\PATH TO WHEREVER\", renames them, and writes the filename and data line by line into the original name, then deletes the file when done. #Magoo's answer clearly tops mine, but a different solution should help you understand different ways to solve future desires.
I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?
No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.
In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity
I would like to rename my video files as per the resolution they are in, e,g for a video 'bla bla.mp4' in 1080p, I would like to rename it to 'bla bla [H.264 1080p]. The script should automatically be able to detect the resolution of the video, and also if the file has been already renamed it should not rename it.I wasn't able to find a way to check for the resolution, so I tried to use this for 1080p files:
FOR /r %%a in (*.mp4) DO (IF EXIST *[H.264*.mp4 (
ECHO Already done)
ELSE (
REN "%%~a" "%%~na [H.264 1080p].mp4"))
But what it does is it checks for the same file again and again which has already been renamed and therefore the reply always is 'Already done'.
This question is beyond the scope of an SO question. Nevertheless I will answer it, because today is sunday.
download and install mediainfo command line version
add the path to the mediainfo binaries to your system or user PATH environment variable
copy the rename script, replace the path to your video folder, there is a safety echo before the rename command, remove it if the output looks good
the script tests for already-exists and already-processed files (suggested by Peter Wright)
rename script:
#echo off & setlocal
cd X:\video\path
for /f "delims=" %%i in ('dir /b /a-d *.mp4') do (
set "fnameo=%%~ni"
set "fnamee=%%~xi"
set "Height="
for /f "delims=" %%j in ('mediainfo "--Inform=Video;%%Height%%" "%%~i"') do set "Height=%%j"
setlocal enabledelayedexpansion
call set "selftest=%%fnameo:[H.264 !Height!p]=%%"
if "!selftest!" equ "!fnameo!" if not exist "!fnameo! [H.264 !Height!p]!fnamee!" (
echo rename "!fnameo!!fnamee!" "!fnameo! [H.264 !Height!p]!fnamee!"
)
endlocal
)
output example:
rename "Handbrake.0.9.8.mp4" "Handbrake.0.9.8 [H.264 800p].mp4"
rename "Hybrid.2012.10.21.1.mp4" "Hybrid.2012.10.21.1 [H.264 800p].mp4"
rename "Womble.mp4" "Womble [H.264 1080p].mp4"
There was a very similar question here:
Windows batch file renames filename unexplainably in FOR loop
Peter Wright had posted a solution with a very good explanation:
Try
for /f "delims=" %%a in (' dir /b /a-d *.mp3') do ( The problem is
that your original syntax finds the next filename - which finds your
renamed file since the new filename (with the prefix) is logically
'greater than' the old.
Using dir builds a list of filenames, and processes the list once it
has been completely built - hence the list doesn't vary as the files
are renamed.
The "delims=" clause ensures that the default parsing of the filename
is ineffective - otherwise, the filename would be interpreted as a
series of [space-separated] tokens.
answered Jun 20 at 4:28 Peter Wright
The IF EXIST conditional will always pass after the first file is renamed, since as written it will check for the existence of any file in the directory that contains "[H.264" in the file name, not just %%a.
Consult Batch file: Find if substring is in string (not in a file) to learn how to find "[H.264" in %%a, which would cause the intended result.
I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)