Windows Power Shell - rename files and move

hi people;

i want to make a file/folder operation as follows.

  • i have 41 folders in Windows and each of them have same-named files (~200 files each) inside.
  • i want to stack these files together in a folder but Windows is asking to "overwrite" (as usual) since the file names are the same.
folder "1":
1.jpg, 2.jpg, 3.jpg, ..., 200.jpg

folder "2":
1.jpg, 2.jpg, 3.jpg, ..., 170.jpg
...
...
folder "41":
1.jpg, 2.jpg, 3.jpg, ..., 210.jpg

and i just need these files (41x~200 = ~8400) in any folder. new file names are not important.

folder "X":
pic1.jpg, pic2.jpg, ..., pic8400.jpg

i donot know how can do this in Windows. but the most logical is to use a "shell code"

cd folder1
ls *.jpg | sed 's/..*/mv "&" "1_&"/' | ksh

double quotes is for spaces in file names
..* is for blank lines
ls is dir /B /A
mv is rename
sed is portable to dos.
ksh is cmd

This is my first ever - and judging the the utter stupidity and weirdness of this crazy language - my last ever PowerShell script:

$Dir = get-childitem . -recurse
$List = $Dir | where {$_.extension -eq ".jpg"}
$c=1
foreach( $i in $List ) {
  $sfile = $i.DirectoryName + "\" + $i.Name
  $tfile = "newdirectory/pic" + $c++ + ".jpg"
  copy-item $sfile $tfile
}

Replace the bits in bold red to whatever tickles your fancy.

... and I thought the C-shell was strange - come back, all is forgiven :smiley:

edit: I've been playing with PowerShell a bit more, and feel that I judged it too harshly. I'm almost embarrassed to admit it, but it's actually quite nice (weird, but nice) when you get the hang of it :slight_smile:

1 Like