The following few lines output the names of certain files in a folder, a delimiter, and a timestamp.
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do echo %indent%%fileBullet% %%F%delimeter% %%~tF
So, produces something like this
Response.docx; 02/07/2013 12:13 PM
I'd like to remove the time portion of the timestamp (so date only), followed by how many days old the file is. So
Response.docx; 02/07/2013; 14
I've found some fairly lengthy solutions online that contain a dozen or so lines. Is there a short and sweet approach?
Here's something shorter and sweeter. It's not as short and sweet as you'd like, but at least it's not 12 lines of code. :)
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do call :datediff "%indent%%fileBullet% %%F%delimeter%" %%~tF
goto :EOF
:datediff
echo wscript.echo DateDiff^("d", "%2", Date^(^)^)>"%temp%\dd.vbs"
set /P i="%~1 %2%delimeter% "<NUL
cscript /nologo "%temp%\dd.vbs"
del /q "%temp%\dd.vbs"
Related
i have 1000 photos with extension .jpg and every name of photo is a code like this 12345.jpg.
And i have 1000 folders with names like this: text_text_text_code(000000)
the job is put every photo in every single folder, i can solve this, but not working very well.
I found the problem, but i don't know how to solve.
in name folder i have text_text_text_code(000000), i need the code to stop when it arrives at character (.
#echo off
chcp 1250
echo.
echo.
cd "C:\Users\folderexample\Desktop\123"
for /r "C:\Users\folderexample\Desktop\imagem" %%a in (*.jpg) do (
for /f %%b in ('dir /b *"%%~na"*') do copy "%%a" "%%~fb"
)
I would like the search to go to the character ( and go to next search up until finish photos.
I need to set Today and yesterday's date in a variable in a fixed format YYYYMMDD.
For today date, when i did
SET TODAY=%date:~10,4%%date:~4,2%%date:~7,2%
it worked and displayed '20190426'.
But how to set yesterday's date so I get it in the format - 20190425 ?
Update The original unix and linux tags were later changed to cmd and batch-file, which this Linux / Bash / sh solution won't apply to.
To get yesterday's date:
$ date +%Y%m%d --date yesterday
20190425
To get it into a var:
$ var=$(date +%Y%m%d --date yesterday)
$ echo $var
20190425
There are literally hundreds/thousands of questions just here on SO.
I suggest you use a PowerShell one-liner for this, which you can call from a batch file as follows:
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'{0:yyyyMMdd}' -f (Get-Date).AddDays(-1)"
`) do set YESTERDAY=%%A
%YESTERDAY% will then contain 20190824 when invoked on 25 August 2019, for instance.
A slightly longer variant, incorporating both today and yesterday in only one PowerShell invocation.
:: Q:\Test\2019\04\26\SO_55862158.cmd
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'yesterday={0:yyyyMMdd}' -f (Get-Date).AddDays(-1);'today={0:yyyyMMdd}' -f (Get-Date)"
`) do set "%%A"
The PowerShell part issues two lines
yesterday=20190824
today=20190825
which are parsed by the for /f and set as environment variables yesterday/today respectivly.
You can use this batch/vbs hybrid, you need to save it as a .bat or .cmd extension file.:
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%yyyy%%mm%%dd%"
echo %final%
Note, you can toggle the number of days in the set day=-1 line.
Previously posted answers are not pure Batch-file solutions... You may use the method explained at this answer or just use this simpler approach:
#echo off
setlocal
set /A "YYYY=%date:~10,4%, MM=1%date:~4,2%, M=MM-100, DD=1%date:~7,2%, D=DD-100"
echo TODAY: %YYYY%%MM:~1%%DD:~1%
set /A "C1=!(D-=1),M-=C1*(1-12*(C2=!(M-1))),YYYY-=C1*C2,MM=100+M,DD=100+(D+=C1*(30+((M+(M>>3))&1)-!(M-2)*(2-!(YYYY%%4))))"
echo YESTERDAY: %YYYY%%MM:~1%%DD:~1%
I need a script that checks the name of a CDG file and if is not present in the same folder another file with the same name in .mp3, then deletes the cdg file.
I thought to implement it with this simple code:
#echo off
for /R %1 %%f in (*.cdg) do (
if exist %%~nf.mp3 (
del %%f
)
)
But it return me with a syntax error in IF construct.
I tried to google it but it seems no-one had same problem (or i'm really bad in using google)
Can please someone tell me where i'm wrong?
Ok, heres an easy way of doing this with forfiles if your using Windows 7:
forfiles /m "*.cfg" /c "cmd /c if exist #fname.mp3 del #path"
That alone should work fine, and it's short enough to be typed directly in cmd.
Mona
It may be due to the name of the file containing spaces. If so then:
#echo off
for /R %1 %%f in (*.cdg) do (
if exist "%%~nf.mp3" (
del %%f
)
)
NB : Quotation Marks
Also, You should look into forfiles (since you'll only be needing one line of code) but that will only work if your using Windows 7.
Mona
Give this a whirl:
#echo off
for /R "%~1" %%f in (*.cdg) do (
if exist "%%~dpnf.mp3" (
del "%%f"
)
)
I've created this very simple batch file for the sake of testing a concept I'm hoping to utilize. I need to recursively delete all of one type of file except in folders with a specific name. Here's my code:
:recur
FOR /f %%a IN ('DIR /b') DO (
IF EXIST %%a\NUL (
IF ["%%a" NEQ "subtest2"] (
ECHO %%a
CD %%a
CALL :recur
CD ..
)
)
COPY "*.cfm" "*_copy.cfm"
REM DEL "*_copy*.cfm"
)
Right now I'm just testing using copy instead of delete. Basically, this should create a copy of all the .cfm files except in the folder "subtest2". Right now it's recursively making the copies everywhere, including subtest2. How do I stop this?
The structure of my base directory is:
TestFolder
---subtest1
------test.pdf
------test.txt
------test.cfm
---subtest2
------test.pdf
------test.txt
------test.cfm
---test.pdf
---test.txt
---test.cfm
---recur.bat
The square brackets are not balanced on both sides of the IF comparison, so it can never match. The brackets are not part of the IF syntax. If present, they simply become part of the string that is compared. The same is true for any enclosing quotes. Remove the square brackets, and it will work (assuming there are no other problems)
Here is a simple method to accomplish your goal. I've prefixed the DEL command with ECHO for testing purposes:
for /r /d %%F in (*) do echo %%F\|findstr /liv "\\subtest2\\" >nul && echo del "%%F\*.cfm"
The FOR /R /D simply recurses all folders. The full path of each folder is piped into a FINDSTR command that looks for paths that do not contain a \subtest2 folder. The ECHO DEL command is only executed if the \subtest2\ folder is not found in the path.
Remove the last ECHO when you have confirmed the command gives the correct results.
Change %%F to %F if you want to run the command on the command line instead of in a batch file.
for f in `find . -path YOURDIR -prune -o print`
do
rm whateveryouwanttodelete
done
the find command in backticks finds all files but ignores the directory -prune you want to ignore. Then in the body of the loop you nuke the files. You can do even better with
find . -path YOURDIR -prune -o -print0 | xargs -0 rm -f
no need for the loop. DISCLAIMER: I haven't tested it so perhaps you want to start adopting it with cp instead of rm.
You can try this:
#echo off&setlocal
for /r /d %%i in (*) do (
pushd "%%i"
echo(%%i|findstr /ri "\\subtest2$" || COPY "*.cfm" "*_copy.cfm"
popd
)
I need to rename multiple files in a loop and include a time stamp... My struggles begin with updating the timestamp for each file so the file has a unique name.
setlocal enabledelayedexpansion
SET date=%date:~-4,4%%date:~-10,2%%date:~-7,2%
for /f %%a in ('dir /b TCA_*') do (SET
time=%time:~-11,2%%time:~-8,2%%time:~-5,2%%time:~-2,2%
ren %%a %date%TCA_%time%.txt)
Thanks
While in a loop, variables don't update. To circumnavigate this, the setlocal enabledelayedexpansion script was added.
Basically, if you want them to update, you need to encase them in exclamation marks (!) instead of percent signs (%).