Batch command to move files to a new directory - windows

I want to write a batch job that when executed will grab all the files in the C:\Test\Log folder and move them to a new directory in the C:\Test. This new directory will have a name called "Backup-" and CURRENT DATE.
So once completed, the log folder should be empty with all the files now located in the new folder.
I know I would have to use the MOVE command, but have no idea how to dynamically create a new folder, and use the date to name it.

Something like this might help:
SET Today=%Date:~10,4%%Date:~4,2%%Date:~7,2%
mkdir C:\Test\Backup-%Today%
move C:\Test\Log\*.* C:\Test\Backup-%Today%\
SET Today=
The important part is the first line. It takes the output of the internal DATE value and parses it into an environmental variable named Today, in the format CCYYMMDD, as in '20110407`.
The %Date:~10,4% says to extract a *substring of the Date environmental variable 'Thu 04/07/2011' (built in - type echo %Date% at a command prompt) starting at position 10 for 4 characters (2011). It then concatenates another substring of Date: starting at position 4 for 2 chars (04), and then concats two additional characters starting at position 7 (07).
*The substring value starting points are 0-based.
You may need to adjust these values depending on the date format in your locale, but this should give you a starting point.

this will also work, if you like
xcopy C:\Test\Log "c:\Test\Backup-%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%" /s /i
del C:\Test\Log

Related

How to make a folder with name as date_time and copy a specified .bat file into that

How to make a folder with name as date_time and copy a specified .bat file into that.
I want to use .bat file for above stated functionality.
Try using search because working datestamps and DOS have been around for many years.
Try this:
set "newdirectory=c:\%date:/=-%-%time::=-%"
md "%newdirectory%"
will create something like "Thu 01-15-2015-14-41-19.61" (depending on your local date/time settings)
Had to look up substring replace and found this post https://stackoverflow.com/a/6159108/4317867
-Edit, add explanations.
MD using the system variables %DATE% and %TIME% but replace the invalid characters / and :

Windows Batch - separate directory and filename from full file path string

I'd like to separate both a filename and a directory string from inside a full file path variable so i can refer to each separately later in a batch script.
Input Variable: SET "FULL=C:\test\file.txt"
Wanted Output:
FILE: file.txt
PATH: C:\test\
Currently the for loop & syntax is not making a whole lot of sense to me (in this batch scripting language) which is making it harder for me to find a working solution online...
set "FULL=C:\test\file.txt"
for %%a in ("%FULL%") do (
set "filePath=%%~dpa"
set "file=%%~nxa"
)
for loop will iterate over a set of files (only one file in set in this case), and for each of them the code after the do clause is executed.
For each iteration of the for loop and so for each execution of the do clause, the replaceable parameter (the %%a in the previous code) will hold a reference to the file being processed.
This replaceable parameter has some modifiers (that can be seen running for /?) to retrieve the required information from the file. The modifiers are in the form
%% ~ modifier replaceableParameter
In the previous sample code, d modifier is the drive where the file is stored, p is the path (folder hierarchy) where the file is stored, n is the file name without extension and x is the extension. So
%%~dpa = drive and path of the file being referenced by a
%%~nxa = name and extensions of the file being referenced by a

CMD to pick up specific files using a wild card

I have a CMD script I have been working on that reads a folder that has 3 different files in it. They have names like the following:
File1_ddmmyyyy.txt
File2_ddmmyyyy.txt
file3_ddmmyyyy.txt
I can use * to identify the files but the next step that uses the file name (Launching an ETL program) requires that the full file path. It is unable to identify the file (is entered as: file3_*.txt Literally)
Does anyone have and ideas on how I can grab this file based on the Wildcard, then obtain the full name to use later?
Thanks
EDIT - Example-
There is one ETL File executable line that matches up with each of the files based on the file name. for example:
File1_ddmmyyyy.txt is matched with ETL file File1.tf.map (Based on the file name and ETL file name) it is a one to one match (3 files to load and 3 ETL files to load them)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File1*.txt"-tc Server="X";Database="Y";Table="dbo.File1" "Y:\MapLocation\File1.tf.xml" (FileName = File1_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File2*.txt"-tc Server="X";Database="Y";Table="dbo.File2" "Y:\MapLocation\File2.tf.xml" (FileName = File2_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File3*.txt"-tc Server="X";Database="Y";Table="dbo.File3" "Y:\MapLocation\File3.tf.xml" (FileName = File3_ddmmyyyy.txt)
In the executable line of code for the ETL program instead of referencing the actual file name (Needed to run) it is trying to run File1*.txt Not filling in the variable.
My only thought would be since I have only 3 files and three ETL files that match base on the root file names I can place each of the files full names in a variable prior to this then use each of them in the executable ETL lines.
Not sure if this would work or how to do it. Let me know if this helps.
Hmm - started off clear as mud.
Then Taz arrived with his mixmaster on Turbo...
What congeals from this appears to be:
There's a directory "Y:\FileLocation\yyyy_mm_dd which contains 3 text files FileNddmmyyyy.txt
where dd is day number, mm month number, yyyy 4-digit year number and N will be 1..3.
The requirement from there is to build an appropriate command using the template
DJENGINE -sc "Y:\FileLocation\yyyy_mm_dd\FileN_ddmmyyyy.txt" -tc Server="X";Database="Y";Table="dbo.FileN" "Y:\MapLocation\FileN.tf.xml"
where dd,mm,yyyy,N have the same meanings, for N=1..3
#ECHO OFF
SETLOCAL
SET "yyyymmdd=%1"
IF NOT DEFINED yyyymmdd for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do set yyyymmdd=%%x&GOTO gotdate
:gotdate
SET "yyyymmdd=%yyyymmdd:~0,8%"&SET "yyyy=%yyyymmdd:~0,4%"&SET "mm=%yyyymmdd:~4,2%"&SET "dd=%yyyymmdd:~6,2%"
FOR %%a IN (1,2,3) DO ECHO(DJENGINE -sc "Y:\FileLocation\%yyyy%_%mm%_%dd%\File%%a_%dd%%mm%%yyyy%.txt" -tc Server="X";Database="Y";Table="dbo.File%%a" "Y:\MapLocation\File%%a.tf.xml"
GOTO :EOF
In the absence of any information about where yyyymmdd come from, the above should fill in the values for today.
If the procedure is provided with a parameter in the format yyyymmdd like this:
thisbatch 20140726
then the procedure will generate lines for July 26th 2014 (note that the value provided here is not checked for validity)
The required DJENGINE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DJENGINE to DJENGINE to actually execute the program on the files. This presumes that DJENGINE is an executable, not a batch procedure.

Why file cannot be found to copy

I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/

Renaming files of variable length in DOS

I'm trying to rename files that have '#' to '_'.
I understand that there is a straigt forward way of replacing the nth character in a file.
How do we rename files , if # symbol is present in different places in different files
For example, assuming the below files are present in a directory
a#file.txt
asdf#kfi.png
uiuydfjfk#kdi.txt
I want the output to be like this one
a_file.txt
asdf_kfi.png
uiuydfjfk_kdi.txt
Is there anyway to accomplish this ?
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
It creates renfile.bat for you to examine for errors, and then execute.
dir *#* /b |repl "(.*)(#)(.*)" "ren \x22$&\x22 \x22$1_$3\x22" x >renfile.bat

Resources