mrjayviper
mrjayviper

Reputation: 2388

how to move bulk move files and then rename them in sequential order?

to test, I'm trying to move c:\Users\myuser\Downloads\test.txt to c:\Users\myuser\Documents\mynewname0001.txt.

The destination folder path is dynamic so for my test I'm using a variable

My final aim is to use GCI + ForEach-Object to move lots and lots of files. But I'm testing with a single file for now.

Here's my script:

$_test = "c:\Users\myuser\Documents"
$_i = 1

Move-Item -Path "$($_test)\test.txt" -Destination "($($_test)\temp\'newname{0:D4}' -f $_i++)"

This is the error I'm getting

Move-Item : Cannot find drive. A drive with the name '(c' does not exist.

I can move the files if I don't order them using:

Move-Item -Path "$($_test)\test.txt" -Destination "$($_test)\temp\newname.txt"

But I do not know how to introduce the auto incrementing code with it.

Can you please help? Thanks

Upvotes: 0

Views: 91

Answers (1)

Krisz
Krisz

Reputation: 743

Try to evaluate the format string separately:

Move-Item -Path "$($_test)\test.txt" -Destination "$_test\a\$('newname{0:D4}' -f $_i++)"

Upvotes: 1

Related Questions