I’ve created a batch file to move files from one folder directory to a network directory.

this is what I have

@echo

MOVE c:\Test*.* S:\admin\test

TITLE “MOVE FILES”

This works fine but what I would like to do is everything thats in the current C:\Test directory when it moves add the date to the end.

OR another solution if there is a file already in s:\Admin\Test then I want it to not overwrite the exisiting but to incriment the new file with a 1 or 2 or 3 what ever.

Any help would be great

4 Spice ups

Dates in DOS batch are a pain to do. Have you considered powershell? With PS you can increment the folder number easier too.

1 Spice up

For DOS batch you need to use the “FOR” with tokens

http://ss64.com/nt/for_f.html

Something like this:

for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo "%%I" "%%J" "%%K"

Then use %I %J and %K in your “MOVE” command string

1 Spice up

Jeff1792 thanks for the quick response…Only thing I have ever used powershell for was to pull data from Office365. Would you happen to know how to code it and save it. Basically what I need to do is every hour have a task scheduler run this batch or powershell file to check c:\Test and if there is files move them to the other network folder.

Thanks,

Jeff

Basically it’s the same:

$myVar = Get-Date -format M.d.yyyy
echo $myVar

result = 8.7.2014

You can make the separator anything you want (well dos file name rules apply) and you can do 2 digit dates like this

$myVar = Get-Date -format MM-dd-yyyy
echo $myVar

See here for more rules on “Dates”

You can count the items inside your directory using something like this:

$items = Get-ChildItem -Path "c:\test" 
foreach ($item in $items)
{
      if ($item.Attributes -ne "Directory")
      {
            //count here
      }
}

Then move the folder if necessary.

So this would be my file

$myVar = Get-Date -format MM-dd-yyyy

echo $myVar

MOVE c:\Test*.* S:\admin\test

TITLE “MOVE FILES”

Actually It did the same exact thing that has already happened no date added to it. and if the file exisit it does not add it

Just returns the date to the screen

Does the PS script run like a Batch file if executed from Task Scheduler?

it doesn’t really show to the user?

No your move command needs to be something like this:

MOVE c:\test*.* s:\admin\test-$myVar

The $myVar should append the date to the folder name “test-” so that your new folder should look like “test-08-07-2017”

Yes powershell will run like a batch file. Depending on what you’r doing inside you may need special permissions. I sometimes need to include “-ExecutionPolicy Bypass” to the command line.

A typical command line for task manger might look like this:

"powershell -ExecutionPolicy Bypass “<scriptName>”

This is over my head bunch of red lines…

$myVar = Get-Date -format MM-dd-yyyy

echo $myVar

MOVE C:\Test*.* S:\Admin\Test-$myVar

This is what I get and It is not moving at all

MOVE : The file exists.

At line:3 char:1

  • MOVE C:\Test*.* S:\Admin\Test-$myVar

  • 
    
  • CategoryInfo : WriteError: (C:\Test\New Journal Document.jnt:FileInfo) [Move-Item], IOException

  • FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

MOVE : The file exists.

At line:3 char:1

  • MOVE C:\Test*.* S:\Admin\Test-$myVar

  • 
    
  • CategoryInfo : WriteError: (C:\Test\New Mic… Worksheet.xlsx:FileInfo) [Move-Item], IOException

  • FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

MOVE : The file exists.

At line:3 char:1

  • MOVE C:\Test*.* S:\Admin\Test-$myVar

  • 
    
  • CategoryInfo : WriteError: (C:\Test\New Microsoft Word Document.docx:FileInfo) [Move-Item], IOException

  • FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

MOVE : The file exists.

At line:3 char:1

  • MOVE C:\Test*.* S:\Admin\Test-$myVar

  • 
    
  • CategoryInfo : WriteError: (C:\Test\New Text Document.txt:FileInfo) [Move-Item], IOException

  • FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

Well you can go back to DOS batch. You move really wants to use move-item command

Sorry, I got you off to a bad start…

No problem mate. Cheers for your help.

Sorry got busy. You want to do something like this in powershell

$date = get-date -format MM-dd-yyyy
$path =  "c:\test-" + $date 
move c:\test $path

If you wanted to do this in a batch file you would have to use a for loop, something like this would work:

@CLS
@echo off

:: set stamp variable for file rename during move
Set stamp=%date:~4,2%.%date:~7,2%.%date:~10%

:: This allows variables to be expanded in for loops, you have to use ! around variables instead of % sign
setLocal EnableDelayedExpansion

FOR /R C:\test %%G IN (*.*) do (

	Set base=%%~nG
	Set ext=%%~xG
	copy "%%G" "S:\admin\test\!base!-%stamp%!ext!")

Change the copy to move once you confirmed that it works. I’d also use unc instead of S:\ in a script.

2 Spice ups

Very nice Brian_Duffy!! I really like this! I can see this helping me out on some things as well…

no problem, some further reading: