Page 1 of 1

Batch script to move files older than 1 day

Posted: 26 May 2016 14:37
by Megsie
Hello all,

I've been trying to modify a windows batch script to move .txt files that are older than one day into a different folder, but have been unsuccessful. Could someone please help?

Here's what I've written so far:

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd -c move @file U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"

It doesn't give me any errors, but it also doesn't move the files.... Any ideas?

Re: Batch script to move files older than 1 day

Posted: 26 May 2016 15:22
by Squashman
cmd.exe use slashes for its options.

Re: Batch script to move files older than 1 day

Posted: 26 May 2016 15:32
by Megsie
My delete script without slashes works fine, not sure if this is based on versions...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -s -m *.txt* /D -2 /c "cmd /c del @path"

Re: Batch script to move files older than 1 day

Posted: 26 May 2016 15:36
by Megsie
When I run my move script it returns

"Microsoft Windows [Version 6.1.7601]
Copyright (C) 2009 Microsoft Corporation. All rights reserved."

Re: Batch script to move files older than 1 day

Posted: 26 May 2016 17:06
by Squashman
Megsie wrote:When I run my move script it returns

"Microsoft Windows [Version 6.1.7601]
Copyright (C) 2009 Microsoft Corporation. All rights reserved."

Because you are using cmd -c. I told you that cmd.exe uses slashes just like the 2nd example your delete uses.

Re: Batch script to move files older than 1 day

Posted: 27 May 2016 16:22
by Megsie
Ok, I've changed my code as follows:

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"

But now it's telling me "The syntax of the command is incorrect."

Re: Batch script to move files older than 1 day

Posted: 27 May 2016 22:19
by Squashman
And what is the rules about file names or paths with spaces in them?

Re: Batch script to move files older than 1 day

Posted: 30 May 2016 11:38
by Megsie
I'm not a programmer or script writer, I don't know the rules.

I've tried putting underscores instead of spaces and adding extra quotations, but the script doesn't work...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\""


...hence I came to a tip forum for help.

Re: Batch script to move files older than 1 day

Posted: 30 May 2016 18:51
by elias
One time is instead of directly doing "cmd /c move @file", I would do a "cmd /c echo move ....".

Simply add "ECHO" to see the command that will be executed. This will let you troubleshoot what's the problem and adjust the command accordingly.

You may want to resort to using "FOR /F" with the "usebackq" switch as a secondary step.

Check the examples here https://github.com/PassingTheKnowledge/Batchography/ especially the "*token*.bat" examples.

Re: Batch script to move files older than 1 day

Posted: 01 Jun 2016 14:25
by douglas.swehla
Megsie wrote:I'm not a programmer or script writer, I don't know the rules.

If you're going to be doing any of this, it's worth learning the rules, or at least the basics. Here is a nice overview on quotes and other special characters: http://ss64.com/nt/syntax-esc.html

Megsie wrote:I've tried putting underscores instead of spaces ...

If a file is named "My File.txt", and you refer to it as "My_File.txt", that will not work, because the file "My_File.txt" does not exist.
If you rename "My File.txt" to "My_File.txt", then you can refer to it with or without quotes ("My_File.txt", My_File.txt), and it will be fine either way.

Megsie wrote:I've tried . . . adding extra quotations, but the script doesn't work...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\""

Your quoting looks okay to me. You've quoted the source and destination directory paths to handle spaces, and you've quoted the entire command string, as required by the FORFILES command.

I think the problem is with how you're referring to the file to be moved in the MOVE command.
  • The @file variable returns only the name and extension of the file, e.g. "MyFile.txt".
  • The @path variable returns the full path to the file, e.g., "U:\My Documents\1. RPC\AutoMed Archive\MyFile.txt".
If the batch file is not located in the same folder as the files, the MOVE command will be trying to act on local files that don't exist:

Code: Select all

move C:\path\to\batchfolder\"MyFile.txt" "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"
rem The command fails, because file C:\path\to\batchfolder\"MyFile.txt" does not exist.


Both variables return an already-quoted string, so you needn't requote them: @path is fine, "@path" is overkill.
Replace @file with @path, test it using the ECHO command as elias suggested, and let us know how it goes.