I cannot append the files together using bcp command. I am getting one file and changing the name of it to todays date, every half hour a file comes in which needs appending to the renamed txt which has the date stamp in the file. once appended that file is move to another folder.
This occurs everyday moving the old txt into another folder and starting the process again. I can't manage to get this into one command script so I can put it into two batch files.
The first renames the file and clears out any old files from two locations, one is yesterdays file and the other is an archive folder for anything over 100 days (days doesn't matter as I am in testing at the moment):
rename C:\Testing\WithRCD\data_list201 CM_Exclusions_%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.txt
forfiles /p c:\Testing\WithRCD\ /m *.txt /d -1 /c "cmd /c move #FILE c:\Testing\WithRCD\Archive\"
forfiles /p "c:\Testing\WithRCD\Archive\" /m *.* /d -100 /c "cmd /c Del c:\Testing\WithRCD\Archive\"
This seems to work but when trying to append the file together they will not, I am trying to use the following code:
copy /Y/V/DS data_list201+CM_Exclusions_%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.txt
or even
copy data_list201+CM_Exclusions_%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.txt TempFile.txt
DEL CM_Exclusions_%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.txt
REN TempFile.txt CM_Exclusions_%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.txt
but nothing I have tried is working.
Related
I'm using forfiles in a batch-file to find all files changed within the last day and save them to an archive directory.
The script works perfectly to pull files from one directory to another, however it does not do the desired function when there are sub directories.
e.g. I have a Source location and an Archive directory. In the source directory there is the following structure:
C:\Temp\Source\test1.txt
C:\Temp\Source\Folder\test2.txt
The Archive directory is simply the below location with no files/sub folders in it:
C:\Temp\Archive
My current use of ForFiles picks up both test1.txt and test2.txt and dumps them into the Archive directory, which is an undesired result:
C:\Temp\Archive\test1.txt
C:\Temp\Archive\test2.txt
What I want to do is preserve the folder structure and get this result:
C:\Temp\Archive\test1.txt
C:\Temp\Archive\Folder\test2.txt
This is the code I'm currently using to achieve the undesired result is:
FORFILES /S /p %source_dir% /m *.* /d +%datedsub% /c "cmd /c copy #path %archive_dir%"
%datesub% is just my variable to decide what date to copy from.
I have noted that I could achieve this by using the #relpath parameter, removing the quotes and .\, then appending it to the %archive_dir% so that I'm specifying the entire destination path rather than just the Archive location.
My problem is that I do not know how to manipulate the # variables whilst within a ForFiles command.
I have a batch-file which works as intended, deleting files with a specified string or extension, but I want to keep a record of what was deleted.
I want to take the output of the command and write it to a .txt file on my desktop before running the actual command:
if /I %CONFIRM%==y (
(del /F /S %FOLDER%\*%DELETE%)>"C:\Users\%username%\Desktop\deleted.txt"
del /F /S %FOLDER%\*%DELETE%
)
In this case %DELETE% is the user input variable value bak.
If I try to delete .bak files it still deletes the files but gives me the following result and wont write to text file:
Could Not Find C:\Users\USERXYZ\Desktop\*bak
Any ideas?
Perhaps
set /p "confirm=delete %FOLDER%\*%DELETE% ? "
if /I %CONFIRM%==y (
(ECHO del /F /S %FOLDER%\*%DELETE%)>"C:\Users\%username%\Desktop\deleted.txt"
(dir/S/B %FOLDER%\*%DELETE%)>>"C:\Users\%username%\Desktop\deleted.txt"
del /F /S %FOLDER%\*%DELETE%
)
may assist.
In your code, the first del command deletes the file AND should create the deleted.txt file containing the names of the deleted files. Since those files have now been deleted, then the second del command quite correctly reports that it could not delete the files since they are no longer there.
Note that > will create a NEW file, whereas >> will append to an existing file (or create a new file if none already exists)
The echo del in this code shows the command that is about to be executed - you may or may not want this - idk. However the > puts this into a NEW file.
I am a rookie at programming.
I have created a batch script to move files from one location to other based on date.
I want to move all files expect the files having modification date 3 days lesser than the current date.Also I want to log the files which has been moved in a new text document.
I am able to move the files but not able to log the moved files.
Command which i used is
forfiles /p C:\Users\Desktop\batchtest\ /s /m . /d -3 /c "cmd /c move #FILE \"C:\Users\Desktop\nov""
What i need is to create a textfile which shows the files moved by the above command.Could someone help me on this?
This is not same to Just deleting the file based on the date I suppose.
You could redirect all command output in to a file, like so:
forfiles /p C:\Users\\Desktop\batchtest\ /s /m . /d -3 /c "cmd /c move #FILE \"C:\Users\Desktop\nov"">log.txt
Notice the ">log.txt". That will redirect all non-error output into the provided file. It will create it if it isn't there. If you want to redirect errors, too, put a 2>log.txt >log.txt at the end instead. Source
Got a folder which contains automatically generated batch scripts. For example - a cron job runs a script, grabs all new rows from a database, cURLs to my windows server, writes a batch file to the folder with the specific ID.
I then have a secondary batch file which I need to run all these batch files at once, not sequentially. The reason for this is at any one time, there can be 100 batch scripts in the folder, taking on average 3 minutes each, which is five hours, while testing shows that all 100 are fine running at once, and only takes 5 minutes.
So far I have:
FORFILES /S /P "./batch_files" /M *.bat /C "cmd /c call #file /Q "
Which does loop through all the files, however only runs them sequentially. I was thinking assigning each file name to a variable and using the "&" operator so it would basically be
FORFILES /S /P "./batch_files" /M *.bat /C "cmd /c variable = variable+"&"+#fname /Q "
call variable
But in proper batch file formatting (I suck at CMD).
Also I don't know whether "variable" would be global, as it runs a separate instance of cmd on every forfiles?
Rather than:
Cmd /C Call #file /Q
use:
Start Cmd.exe /Q /C #file
Start begins a process and returns immediately.
You can't. The START command start another process and returns immediately to execute the next line in a Batch file IF AND ONLY IF the executed process is a 32-bits Graphical Windows application. If not, that is the case of CMD.EXE, then START waits for the executed command to end.
A combination of the above answers should help.
Assuming you are wanting to search the contents of folder batch_files located in the root folder would give the following:
FORFILES /S /P "\batch_files" /M *.bat /C "cmd /C start call #file"
EXIT /B
This worked for me.
I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range.
eq.
xcopy /S /D:01-10-2011 *.* C:\todaysFiles
But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat
If you're on Vista/Win7/WinServer2008 you can use robocopy like so:
robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001
On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.
The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).
The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.
To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.
SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if #isdir==FALSE 2> nul forfiles /M #file /D -%DATE_TO% && > con ( echo #path && copy /V #path %DESTINATION% )"
Note: this will copy files in subfolders as well as files in the top-level folder.
The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.
A caveat is that it is based on date modified (original question asked for date created)
Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.
You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]
more info here, and here
Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.
for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)