Help please!
I need to remove time stamp and file number off of the filename using a windows batch script. Thank you! Thank you in advance!
OLD Filename= CAM168-NOTSET.2013.10.01.18.45.45.882.jpg
NEW Filename= CAM168-NOTSET.jpg
#ECHO OFF
SETLOCAL
SET "fname=CAM168-NOTSET.2013.10.01.18.45.45.882.jpg"
FOR %%i IN ("%fname%") DO FOR /f "delims=." %%j IN ("%%i") DO ECHO REN "%%~i" "%%~j%%~xi"
GOTO :EOF
New name merely ECHOed.
If by "remove time stamp and file number", you mean remove everything between the first and last dot, preserving the extension, then you don't need a batch script. A single one liner will do.
This command will rename all .jpg files in the current directory. It will support final names up to length 50, excluding date, number, and extension. Add additional ? if you have longer names, or you can remove some ? if your names are shorter.
for %F in (*.jpg) do #ren "%F" "??????????????????????????????????????????????????%~xF"
Double up the percents if you use the command in a batch file.
You can put any file mask, or list of files, etc in the in () clause.
If you are just going to rename .jpg files, then you can simply use:
ren *.jpg ??????????????????????????????????????????????????.jpg
Refer to How does the Windows RENAME command interpret wildcards? for info on why this works.
Lloyd has an interesting solution that will only rename files that match a template of
name.number.[number.]...ext, expressed as regex. But it requires installation of a third party executable. The same specificity can be achieved easily with pure script using a hybrid JScript/batch utility called REPL.BAT.
Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH, then the following will match all files that match the regex template:
for /f "eol=* delims=* tokens=1,2" %A in (`dir /b /a-d^|repl "(.*?)(\.\d+)+(\.\w+) a" "$&*$1$3"') do ren "%A" "%B"
Double up the percents if used within a batch script.
using renamer (cross-platform file renamer) with these input files:
CAM168-NOTSET.2013.10.01.18.45.45.882.jpg
CAM169-NOTSET.2013.10.01.18.45.45.883.gif
CAM170-NOTSET.2013.10.01.18.45.45.883.jpeg
and this command:
$ renamer --find '/(.*?)(\.\d+)+(\.\w+)/' --replace '$1$3' *
produces these filenames:
CAM168-NOTSET.jpg
CAM169-NOTSET.gif
CAM170-NOTSET.jpeg
Related
I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).
I need to get the filenames and date modified of all the files from a folder.
I had tried using the below command to get same. But it only gives me the list of files present in the folder and the date modified is missing.
Can someone help me out to modify the same and get the filenames and the date modified of the file as well.
dir /b /s > fileslist.csv
From command line, to retrieve the required information
for /r %a in (*) do #echo %~ta %~fa
For each folder from the current one and below (recursive, /r), for each file (that will be referenced by %a replaceable parameter), echo the timestamp of the file (%~ta) and the full name of the file (%~fa)
To use it inside a batch file, percent signs in for replaceable parameters need to be doubled
for /r %%a in (*) do #echo %%~ta %%~fa
To send the output to a file, just redirect the full output of the command
>"file.csv" ( for /r %%a in (*) do #echo %%~ta,"%%~fa" )
note: read the output of for /? to get the reference of all the available options
Based upon your opening post and subsequent comments I'd use the Where command:
(For /F "Tokens=2-3*" %A In ('Where/T .:*.zip') Do #Echo(%A,"%~nxC")>output.csv
You could also quote %A, (if you prefer that particular format).
Here is the file name format. The leading number is the layer and the second number is the material (3D printer).
01118_7.tif,
01118_6.tif,
01118_5.tif,
01118_4.tif,
01118_3.tif,
01118_2.tif,
01118_1.tif,
01118_0.tif
What I need to do is shift the files ending in _1, _4, _6 six places higher. So, 01124_1, 01124_4, 01124_6 while the rest of the files stay the same. I need to do it all the way down to layer 00112_*.
I'd like to do this via a batch file if I can. Was trying to follow a guide but the name format is tripping me up.
Basic excel format
I can't tell if you need to modify file names that appear within a text file, or if you need to rename files. Either way, I have a simple solution using one of two hybrid JScript/batch regex utilities:
Modify filenames within a text file using JREPL.BAT:
jrepl "^\d{5}(?=_[146]\.tif)" "lpad(Number($0)+6,'00000')" /i /j /f test.txt /O -
Rename files within the current directory using JREN.BAT:
jren "^\d{5}(?=_[146]\.tif$)" "lpad(Number($0)+6,'00000')" /i /j
Use call jrepl or call jren if you put the command within a batch script.
It took me awhile to understand that "shift file names six places higher" really means "add six to file name".
#echo off
setlocal EnableDelayedExpansion
set "numbers=/1/4/6/"
for /F "tokens=1,2 delims=_." %%a in ('dir /B /A-D *.tif') do (
if "!numbers:/%%b/=!" neq "%numbers%" (
set "newNum=1%%a+6"
ECHO ren "%%a_%%b.tif" "!newNum:~1!_%%b.tif"
)
)
If the names are not in files, but are lines of text placed in a text file, change the 'dir /B /A-D *.tif' command by the name of the text file.
I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1
In Vista, I want to run a batch script to open the most recent file, based either on last modified date, or the date in the filename. It's for Malwarebytes' Anti-Malware logs in the %username&/appdata/roaming/Malwarebytes/Malwarebytes' Anti-Malware/Logs folder.
Log files are in this format here
mbam-log-2009-03-21 (00-20-21).txt
mbam-log-2009-03-21 (09-42-40).txt
mbam-log-2009-03-21 (11-02-43).txt
mbam-log-2009-03-21 (11-12-01).txt
mbam-log-2009-03-21 (12-01-42).txt
mbam-log-2009-03-21 (12-04-49).txt
mbam-log-2009-03-21 (14-01-41).txt
So its 24-hr format. I read on another page on here, and got this script here..
#echo off
dir *.txt /b /on > systext.bak
FOR /F %%i in (systext.bak) do set sysRunCommand=%%i
call %sysRunCommand%
del systext.bak /y
but it doesn't like the space in the filename.. always get an error.
Anyone have any ideas?
You actually have three problems in the script. The first is that %%i is likely to be set to the words with a filename- this can be fixed by using "delims=" in the for statement.
The second is that you need to quote spacey filenames in you call statement.
The third is that I'm not aware of a /y option for del, perhaps you mean del /f.
Amyway, give this one a shot:
#echo off
dir *.txt /b /on > systext.bak
FOR /F "delims=" %%i in (systext.bak) do set sysRunCommand=%%i
call "%sysRunCommand%"
del /f systext.bak
The following one-line batch file will open the most recently modified file:
for /f "usebackq delims=" %%i in (`dir /b /o-d`) do #start "%%i"&goto :eof
Using dir strikes me as much simpler than trying to dissect the date in the filename. You can also order by filename (as the date format is somewhat ISO-8601-ish is sorts well).
The goto :eof is just there to make sure only the most recent file will be opened and not all in order of date/time.
As for your space problem, surrounding the file name with quotes usually should fix that, but sometimes it's a little difficult to know where they have to be. Also, for by default tokenizes its input at spaces, that's why I included delims= in there which essentially says »Put everything into the variable and don't do any tokenizing«.
I know this is an old post, and I don't mean to revive it - I have an alternative approach to the question.
Malwarebytes' Anti-Malware includes the ability to reroute the log file to an alternate location (folder or specified file).
You can run "%programfiles%\malwarebytes' anti-malware\mbam.exe" /logtofile c:\logs\bob-mbam-log.txt. /logtofolder also works. (the above command assumes you're using a 32 bit system.)
When specifying a file, mbam appends instead of overwriting. So, that may make the log a little more difficult to parse.