Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have found a PowerShell script which is capable of moving 'n' number of files from 'D:source' to 'E:Dest' directory. It will take the first 'n' number of files in the source folder and move them to the specified subfolder. It can create folders as long as they are needed.

Code:

$filesperfolder = 150
$sourcePath = "D:source"
$destPath = "E:Dest"
$i = 0;
$folderNum = 1;


Get-ChildItem "$sourcePath*.jpg" | % {

    New-Item -Path ($destPath + "" + $folderNum) -Type Directory -Force
    Move-Item $_ ($destPath + "" + $folderNum);

    $i++;

    if ($i -eq $filesperfolder){
        $folderNum++;
        $i = 0 ;
    }
}

So far this script is working fine but, how do I set its working directory. I mean, if the script is on 'C: est' folder, then it should search that current folder for '*.jpg' files and create folders on that same directory.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
201 views
Welcome To Ask or Share your Answers For Others

1 Answer

Whilst it may operate from C:Test, you are still providing full addresses to other locations so as long as it has access and permission to those files in D:Source and E:Dest, it won't touch anything to do with C:Test except for run from that location. What is your intention, you haven't actually asked us for any help with anything.

If you want to set the working directory you would use this:

Set-Location "D:SourceFiles"

This way, regardless of where you started your script from, it would work from 'D:SourceFiles'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...