How to find and copy file containing today's date in filename, the easiest way (e.g. cmd.exe) - cmd

I have one folder with 365 files named
01-01.mp3
02-01.mp3
...
25-08.mp3
and so on and I want to copy a file of "today's date" into another folder. Can I do it in Windows Task Scheduler via cmd.exe somehow?
Thank you for your help!

#echo off
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set today=%Day%-%Month%
copy "PATH\TO\FOLDER\%today%.mp3" "PATH\TO\SAVE\FILE\example.mp3"
Save the file as .bat

Related

How to rename files based on CSV file with FOR /F loop?

I'm trying to automate renaming of files based on a CSV such as the one shown below:
Name,FullName
John,JohnDoe
Jane,JaneDoe
Joe,JoeDoe
Let's say I have 3 text files within the same folder of my .bat called John.txt, Jane.txt, Joe.txt and I want to rename John.txt to JohnDoe.txt, etc.
I am getting "The system cannot find the file specified" no matter how much I alter the filepath in my rename. Here is a basic rundown of what I have. What am I doing wrong here or what other way should I approach this? I appreciate all feedback.
#echo off
setlocal enabledelayedexpansion
set csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (!csvpath!) do (
set person=%%g
set name=%%h
echo My name is !person! and my full name is !name!
rename !person!.txt !name!.txt
)
pause
This is how I would do it:
#echo off
set "csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv"
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (`findstr /v /c:":-:-:" "%csvpath%"`) do (
echo My name is "%%g" and my full name is "%%h"
rename "%~dp0\%%g.txt" "%%h.txt"
)
pause
This code is a bit cleaner and more robust, in that file paths and names can have special characters (like &) without breaking the script.
findstr /v /c:"SEARCHSTRING" "FILEPATH" tells findstr to print every line within FILEPATH excluding (/v) lines with SEARCHSTRING. This doesn't really change much from what you had previously, however it is a bit more robust.
In the rename command, I set it to %~dp0 and then the file name, %~dp0 is the path to where your .bat script is.

How to copy files and paste in folder by creating a current date in batch script

I have a bunch of Excel Workbooks that update periodically. I want to copy these files every Sunday to the folder created with current date in the name (e.g. Workbooks-28.06.2017). I have written a batch script but it is not working.
What am I doing wrong?
Code:
#echo OFF
xcopy /s C:\Users\rerraboina\Desktop\tracker automation\Consolidation\test dynamic
for /f "skip=1" %%d in ('wmic os get localdatetime') do if not defined mydate set mydate=%%d
md %mydate:~0,8%
Here's an example:
archive.bat
# ECHO OFF
PUSHD %~dp0
FOR /f "skip=1" %%d IN ('WMIC OS get LocalDateTime') DO IF NOT DEFINED myDate SET myDate=%%d
SET archiveDirName=Workbooks-%myDate:~0,8%
MD %archiveDirName%
XCOPY /S /I /Y "Excel Workbooks" %archiveDirName%\
POPD
"Excel Workbooks" is an example folder which contains multiple subfolders having on *.xlsx file in each.

windows batch unzip to search result location folder

I have a small section of a windows batch script that for every zip file it finds, it unzips it to a specific location, then deletes it. This works fine, however I was looking to amend it so when it finds a zip file, it should extract it to a new folder inside its current folder named "current .zip name + current date-time". But I can't seem to get this working, it looks like it extracts it to the parent folder I specify to search instead of the folder where it is found.
The reason for this requirement is that the .zip will be stored in a different folder every time named after the build, e.g. \server\g$\Dashboard\Results\NightlyBuild\25\ so I would want the zip extracted inside this same folder.
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
for /R \\server\g$\Dashboard\Results\NightlyBuild\ %%f in (*.zip) do
"C:\Program Files (x86)\WinZip\WZUNZIP.EXE" -d %%f "%%~nf-%mydate%-%mytime%\"
if ERRORLEVEL 0 for /R \\server\g$\Dashboard\Results\NightlyBuild\ %%f in (*.zip) do del %%f
Any help is greatly appreciated, thanks!
As #Noodles pointed out in the comments above, %%~dpf is the folder containing the zip.

Copy files from folder of lnk files back into folder

I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal

Windows Batch File to Find String in Filename

How can i find the String in filename using a windows batch command. All systems generated files has dates in the file name like "xxx_2014-04-11_v1.xml", I want to read the file name to verify date before performing any other operation. How can this be achieved?
Something like this:
for /f "delims=_ tokens=2" %%a in ('echo "xxx_2014-04-11_v1.xml"') do echo %%a

Resources