Batch file that picks an x amount of files and copy them to another folder

Viewed 95

I need a script that copies an x amount of files and copies them to another folder.

I tried using PowerShell commands, but I can't find a way to add variables that were determined in the batch file.

Is there a way of doing it all in batch?

My code:

@echo off & cd /d "%~dp0"

set /p var1=Enter first dir: 
set /p var2=Enter second dir: 
set /p num=How many files: 

echo %var1%
echo %var2%
echo %num%

powershell -Command "& {ls $env:var1 | Get-Random -Count $env:num | Copy-Item -Destination $env:var2}"

pause

I'm sorry if this is a simple question, but I'm kinda new to batch and still learning.

3 Answers

Use $env:The_Name_Of_Your_Variable_Set_In_You_Bat_Code_Here in PowerShell

@echo off & cd /d "%~dp0"

set /p "var1=Enter first dir: "
set /p "var2=Enter second dir: "
set /p "num=How many Files: "
::Var (Folder Directory)

echo %var1% & echo %var2% & echo %num%

PowerShell -nop -c "ls -file $env:var1 | Get-Random -Count $env:num | Copy -Destination $env:var2"

pause

Here is a script for powershell to move random files from one dir to another.

#Randomfilemove.ps1
$fromdir = Read-Host 'Enter first dir'
$todir = Read-Host 'Enter second dir'
$numb = Read-Host 'How may files'

if(!(Test-path "./$fromdir")){
  throw "$fromdir dosen't exist"
}
if(!(Test-path "./$todir")){
  throw "$todir dosen't exist"
}
if((Get-ChildItem "./$fromdir" | Measure-Object).Count -lt $numb){
  throw "Thats too many items"
}

$randomorder = Get-ChildItem -path "./$fromdir" -File | Get-Random -Count $numb

for ($i = 0; $i -lt $numb; $i++) {
  Copy-Item "./$fromdir/$($randomorder[$i])" -Destination "./$todir"
}

As long as your variables are defined in your , and obviously expand to actual directories, there's absolutely no reason access those variables from itself. The variables are parsed/expanded before being received by PowerShell.

%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "Get-ChildItem '%var1%' -File -Force | Get-Random -Count %num% | Copy-Item -Destination '%var2%'"

Here's an example of your script done using more robust methods:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Pushed=FALSE"
If /I Not "%CD%\" == "%~dp0" (PushD "%~dp0" 2> NUL
    Set "Pushed=TRUE")

:AskSource
Set "var1="
Call :FolderBrowser source %CD% var1
If Not Defined var1 GoTo AskSource

:AskDestination
Set "var2="
Call :FolderBrowser destination 0 var2
If Not Defined var1 GoTo AskDestination

:GetFilecount
Set "count="
Set "d=0"
For /F %%G In ('Dir "%var1%" /A-D/W') Do Set /A count=d,d=%%G 2> NUL
Set "d="
If Defined count (If %count% Equ 0 GoTo :EOF) Else GoTo :EOF

:AskFilecount
For /F "Delims==" %%G In ('"(Set num) 2> NUL"') Do Set "%%G="
Set /P "num=How many files [max %count%]"
(Set num) 2> NUL | %__AppDir__%findstr.exe /R "^num=[123456789][0123456789]*$"^
 2> NUL 1>&2 && If %num% LEq %count% GoTo CopyFiles
GoTo AskFilecount

:CopyFiles
%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile ^
 "Get-ChildItem \"%var1%\" -File -Force | Get-Random -Count %num% |" ^
 "Copy-Item -Destination \"%var2%\""

Pause

If "%Pushed%" == "TRUE" PopD

GoTo :EOF

:FolderBrowser
For /F Delims^=^ EOL^= %%G In (
    '%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile ^
     "(New-Object -COM 'Shell.Application').BrowseForFolder(" ^
     "0,\"Select your %~1 directory.\",1,\"%~2\").Self.Path"') Do Set "%~3=%%G"
Exit /B
Related