Batch script command - batch-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

Related

Delete Folder and contents with contains specific name

I would like to delete the folder and it contents when it contains a specific string in its folder name.
For example,
C:\Documents\System_This_Computer_08-01-Mon_1416
C:\Documents\System_This_Computer_09-01-Tue_1120
C:\Documents\System_This_Computer_10-01-Wed_2315
C:\Documents\System_This_Computer_11-01-Thu_0816
C:\Documents\MyDocus
C:\Documents\ToPrintout
With the above folders, i want to delete the folder which contains strings like System_This_Computer.
So, the output should be,
C:\Documents\MyDocus
C:\Documents\ToPrintout
should only be available. remaining should be deleted.
May I know how to do this?
From my comment, the following should show you all of the directories in C:\Documents which have names beginning with System_This_Computer and have not been modified in the last 10 days.
ForFiles /P "C:\Documents" /M "System_This_Computer*" /D -10 /C "Cmd /C If #isdir==TRUE Echo #path"
Once you are satisfied with the output, change Echo to RD /S/Q to actually remove them.

Bat script-zipping log files and move them to another location without any pop-ups and alerts

I am trying to zip the log files of one location and then move them to another location
set _my_date=%date%``
set _my_date=%_my_date: =_%
set _my_date=%_my_date::=%
set _my_date=%_my_date:/=_%
set _my_date=%_my_date:.=_%
set logpath1="C:\Desktop\ArchivalofLogs\currentlogs"
set arcpath1="C:\Desktop\ArchivalofLogs\currentlogs\ArchiveLogs"
set zippath="C:\Program Files (x86)\WinZip\WINZIP32.EXE"
c:
cd %logpath1%
FORFILES /D -2 /M *.log /C "cmd /c move #path %arcpath1%"
cd /D %arcpath1%
%zippath% a -tzip logs-%_my_date%.zip %arcpath1%\*.log
del *.log
When I am executing this code the log files are getting zipped and moved but it is asking for manual inputs like yes or no buttons.I want it to be executed without any alert boxes appearing while the process runs.Can anyone help me out?
Thanks in advance

bcp append files using command

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.

Batch file to start all documents 3 days old or newer in a folder

I am trying to create a batch file that will:
Start in a minimized state, all documents 3 days old or newer, in a folder.
It appears this must be a combination of the for, forfiles and start /min commands.
I have tried:
This script properly opens in my date range, the first document in the directory but will not open any more documents until I close the first document. It will not start them minimized:
forfiles /p "C:\Users\user\Documents\test" /s /d -3 /c "cmd /c #path"
This opens all documents at once, each in its own command prompt window but does not show the document in its appropriate default program, i.e. Microsoft Word for .doc files. It also does not open them minimized:
forfiles /p "C:\Users\user\Documents\test" /s /m *.* /c "cmd /c Start #path" /d -3
This opens all docs but not minimized or in my date range:
for %i in (c:\Users\user\Documents\test\*) do %i
I do not have a script that will open a documents in a minimized state.
Please help!
Many thanks!

How to copy all files created between two dates using Command prompt?

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
)

Resources