Super User Asked on December 18, 2021
I would like move files to a sub folder inside a folder that has dynamic name.
T:/fixed name/07-28-20/fixed Name/
Where the date (07-28-20) changes every day, so this will change daily.
I was able to create a batch file that moves the files to a specific sub folder within the folder, but unable to add/find a wildcard that would be able to pick up the new date in the folder without changing the batch file everyday.
Here is what it looks like now: move "C:UsersxxxxxxCompany NameLanding PageTuesday".xlsm "NetworkDrivedataTest7-28-202"
Thank you,
Please forgive me if I cover anything you already know in the answer below. =) As a small note, if you want more robust options, PowerShell might be a preferable alternative to batch.
I was unable to add/find a wildcard that would be able to pick up the new date in the folder without changing the batch file everyday.
There might be better options, but below are three possible solutions, depending on your needs.
Same Day Transfers
Assuming the transfers take place on the same day (i.e. you are moving Tuesday's file on Tuesday), you can parse and format the current date (as provided by Windows) to use in your move
command:
ex. move_with_current_date.bat
@ECHO OFF
@REM Turn the current %DATE% (ex. "Sun 07/26/2020") into ex. "07-26-20"
@REM and place it in a new variable called "CurrentDate".
SET CurrentDate=%DATE:~4,2%-%DATE:~7,2%-%DATE:~12,2%
@REM SET DestinationFolder=folder%CurrentDate%subfolder
SET DestinationFolder=C:path with spacesfolder%CurrentDate%subfolder
@REM Optional. Make the destination folder if it doesn't already exist.
IF NOT EXIST "%DestinationFolder%" MKDIR "%DestinationFolder%"
MOVE "file.txt" "%DestinationFolder%"
Where:
%DATE%
returns the current date from Windows in the format ex. Sun 07/26/2020
.
%DATE:~X,Y%
is a substring of %DATE%
. X represents the number of characters to "skip" starting at the beginning of the string (starting from 0)
and Y represents the number of characters to keep after that point. So to extract Sun
from Sun 07/26/2020
, the substring call would be %DATE:~0,3%
(start at position 0 and collect the next three characters).
Reading A Name From A File
Assuming the transfers don't take place on the same day (i.e. you are moving Tuesday's file on Wednesday), you could read a simple text file for a folder name (ex. a given date):
ex. date.txt (no trailing spaces)
07-26-20
ex. move_with_file_contents.bat
@ECHO OFF
@REM Read the contents of ex. date.txt.
@REM Note that this file *cannot* have trailing spaces.
FOR /F "usebackq tokens=* delims=" %%x in ("date.txt") DO SET CurrentDate_Read=%%x
@REM SET DestinationFolder=folder%CurrentDate_Read%subfolder
SET DestinationFolder=C:path with spacesfolder%CurrentDate_Read%subfolder
@REM Optional. Make the destination folder if it doesn't already exist.
IF NOT EXIST "%DestinationFolder%" MKDIR "%DestinationFolder%"
MOVE "file.txt" "%DestinationFolder%"
The usebackq
option in the FOR
loop isn't required if just using ex. date.txt
.
However, using quotes (ex. ""
) for e.g. paths with space will not
work correctly without this option (anything between ""
will be interpreted as a literal string).
Be careful that there are no trailing spaces in your ex. date.txt
file. Trailing spaces will cause malformed paths e.g. folder 7-26-20 subfolder
(there is a space after 07-26-20
). In short, while there can be spaces at the beginning of folder names, spaces at the end of folder names are not allowed.
Note that one possible way to create ex. date.txt
is:
ex. create_date_file.bat
@ECHO OFF
@REM Turn the current %DATE% (ex. "Sun 07/26/2020") into ex. "07-26-20"
@REM and output it to a file called ex. "date.txt".
ECHO %DATE:~4,2%-%DATE:~7,2%-%DATE:~12,2%> "date.txt"
Be aware that there should be no gap between the date string and >
. Otherwise, this will lead to a trailing space issue (above):
@REM Bad. Gap, Trailing Space.
ECHO %DATE:~4,2%-%DATE:~7,2%-%DATE:~12,2% > "date.txt"
@REM Good. No Gap, No Trailing Space.
ECHO %DATE:~4,2%-%DATE:~7,2%-%DATE:~12,2%> "date.txt"
Reading The Last Folder Name From A Directory
If you just have directories marked as dates (ex. 07-26-20
, 07-27-20
) in your destination folder, it may be possible to simply read the last directory name and use that in your move
target:
ex. "folder" layout
[...]
07-25-20
07-26-20
07-27-20
example.txt
[...]
You could then include ex. 07-27-20
in your move
target like so:
@ECHO OFF
@REM Returns only directory (i.e. not file) names in a folder.
FOR /F "delims=" %%x in ('dir /b /ad "folder"') DO SET LastFolder_Read=%%x
@REM SET DestinationFolder=folder%LastFolder_Read%subfolder
SET DestinationFolder=C:path with spacesfolder%LastFolder_Read%subfolder
@REM Optional. Make the destination folder if it doesn't already exist.
IF NOT EXIST "%DestinationFolder%" MKDIR "%DestinationFolder%"
MOVE "file.txt" "%DestinationFolder%"
The caveat here is that this assumes ex. 07-27-20
already exists as a directory in your destination folder before your move
.
Answered by Anaksunaman on December 18, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP