-1

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.

Alagu
  • 1
  • 2
  • The code you have provided does not match the requirements you have laid out. Please correct/update your question with the parameters clearly laid out. – Compo Dec 20 '16 at 12:09
  • My requirement is I have to move files from one location to another location.The criteria here is i have to move files which are older than 3 days. – Alagu Dec 20 '16 at 13:04
  • My requirement is I have to move files from one location to another location.The criteria here is i have to move files which are older than 3 days.For example if 20dec is current date,the source path should have only files of date 19,18dec all the other files should be moved to destination path.Also the files which are moved during this process must also be noted down in a seperate text file. I need to run this Batch file on a daily basis.Kindly help me out With the correct code over here. – Alagu Dec 20 '16 at 13:07
  • How many days does your code look for? – Compo Dec 20 '16 at 13:07
  • It should look for 3 days.It should always contains files which are created today,yesterday and day before yesterday all other files should be moved.This batch file should run daily – Alagu Dec 20 '16 at 13:10
  • If the script is run daily, surely after the first time it is run you'll only have files there which are one day old! If that is the case then you only need a batch file for the latter, the former just needs to be run once either at the command line or in Windows GUI. – Compo Dec 20 '16 at 13:14
  • I have a folder called log files where i recieve 50 files per day,which is the source file.I have to create a script which will move files other than the one created today yesterday and day before yesterday. forfiles /p C:\Users\\Desktop\batchtest\ /s /m *.* /d -3 /c "cmd /c move @FILE \"C:\Users\Desktop\nov"" this move all files greater than expected.I have to note the files moved while this command is executed for that what else i have to add in this code? – Alagu Dec 20 '16 at 13:21
  • I have updated your code in your question to match that in your last comment. – Compo Dec 20 '16 at 13:25
  • I could not see the code could you please enter the same in a new comment? – Alagu Dec 20 '16 at 13:27
  • If for example files named log(15dec),log(16dec) etc are moved from source path to destination path.I need to create a text file stating log(15dec),log(16dec). could you please update the code accordingly – Alagu Dec 20 '16 at 13:46
  • 1
    Possible duplicate of [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – Ken White Dec 20 '16 at 13:47
  • @KenWhite If duplicate could you please help me to find the correct solution? – Alagu Dec 20 '16 at 14:53
  • You properly escaped the opening `"` in your `move` command line like `\"`, but you did not escape the closing one (write: `...\nov\"`)... – aschipfl Dec 20 '16 at 16:02
  • 1
    I linked you to a post that provides the correct solution. ??? – Ken White Dec 20 '16 at 16:19
  • @KenWhite No that was just quite similiar mine is different from that.I believe you didn't understand what i was trying to ask – Alagu Dec 21 '16 at 04:35
  • No, I understand **exactly** what you're trying to ask, and I'm telling you that the answer is in the post I linked as a duplicate. I believe you're not reading the answer to that linked post and thinking about how it could be applied to your problem. – Ken White Dec 21 '16 at 13:26
  • I have read it and so i said it was not the same kinda which i asked,now i found the solution for that.Anyways thanks – Alagu Dec 22 '16 at 04:41

1 Answers1

0

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

gusg21
  • 147
  • 11
  • log.txt file is created but here there is 2 issues for me 1)it shows only 1 file(s) moved. etc what i need is the name of the file moved. 2)I have to run this code on a daily basis so that the log file thus created daily so it should not overwrite,a new log file needs to be created.Kindly help – Alagu Dec 20 '16 at 14:01
  • @Alagu Using specifically the `forfiles` command, you can't display the files moved, so I can't fix your first problem. But as for your second problem, replace the `log.txt` with `log_%date:~10,4%-%date:~6,2%-%date:~4,2%.txt`. That will use the date in your file name. – gusg21 Dec 20 '16 at 14:08
  • Thanks for that.But what I have to do if i Need to make a note of the files that are transfered during the execution of the command?If forfiles cant be used for that what else can be the other options? – Alagu Dec 20 '16 at 14:20
  • You could use `fc C:\Users\\Desktop\batchtest\* C:\Users\Desktop\nov\*` to compare the two directories. It will return an `%ERRORLEVEL%` of a number other than 0 if they're different. – gusg21 Dec 20 '16 at 15:51
  • log_%date:~10,4%-%date:~6,2%-%date:~4,2%.txt is not working for me – Alagu Dec 20 '16 at 15:58