One works, the other doesn't

  • Not using pwsh. Using windows powershell.
  • Both scripts are in the same folder.
  • Both arguments are passed in the exact same way.
  • Both arguments are exactly the same, it is a path with a filename.

Command to launch:
powershell -file "E:\Storage\PROGRAMAS\PORTABLES\SimpleContextMenus.0.0.2\Extensions\CLIPS\[TODO]\A\amateur.ps1"

Argument (for both tests):
"E:\Perfil\Desktop\test1\[saroslfs.simpek] - 2020.09.06 - gannekl Redd - Strong binder (1654)-11 -seg002.mkv"

To test:

  1. Create a dummy file: [saroslfs.simpek] - 2020.09.06 - gannekl Redd - Strong binder (1654)-11 -seg002.mkv"
  2. Create both PS scripts from here.
  3. Launch each and see if it works for you.

WORKS:

#DEBUG!!!
#Start-Transcript -path 'E:\Perfil\Desktop\logfile.txt'

# CHOICE TEMPLATE - CREAR CARPETA
function Show-Menu
{
    Write-Host "SI: Presiona 'S' para crearla."
    Write-Host "NO: Presiona 'N' para salir."
}

# MOVE Y COMPRUEBA
$MYFILE=$args
$DEST="E:\Perfil\Desktop\testvid"
# EXISTE LA CARPETA DESTINO?
if (Test-Path -LiteralPath $DEST) {
	ForEach-Object -process {
    Move-Item -LiteralPath $MYFILE -destination $DEST
	}
}
# ERROR HANDLER
else {
	Write-Host "ERROR EN PATH DE DESTINO: $DEST no existe. Queres crearla?"
	do
	{
     Show-Menu
     $input = Read-Host "Elegi"
     switch ($input)
     {
           's' {
                MKDIR $DEST
				cls
                'Hecho, volve a correr el script para mover el archivo'
				timeout 2
				exit
           } 'n' {
                exit
           }
     }
     pause
}
until ($input -eq 'n') 
}

DOESN’T WORK:

#DEBUG!!!
#Start-Transcript -path 'E:\Perfil\Desktop\logfile.txt'

# CHOICE TEMPLATE - CREAR CARPETA
function Show-Menu
{
    Write-Host "SI: Presiona 'S' para crearla."
    Write-Host "NO: Presiona 'N' para salir."
}

# VARIABLES
$MYFILE=$args
$DESTVID="E:\Perfil\Desktop\testvid2"
$DESTPIC="E:\Perfil\Desktop\testpic2"
$TYPE='.jpg', '.jpeg', '.png', '.webp', '.bmp', 'tiff'
$EXTN=[IO.Path]::GetExtension($MYFILE)

# PROCESA IMAGENES
if ($EXTN -in $TYPE) {
	if (Test-Path -LiteralPath $DESTPIC) {
		ForEach-Object -process {
			foreach ($ITEM in $MYFILE) {
				$FILENAMEEXT=split-path -path $ITEM -leaf
				$FILENAME=(Get-ChildItem -path $ITEM).BaseName
				$FILEDIR=split-path -path $ITEM
				if (Test-Path -literalpath $DESTPIC\$FILENAMEEXT) {
					$RAND=Get-Random 
					$NEWNAME=($FILENAME + '_' + $RAND + $EXTN)
					Rename-Item -Path $ITEM -NewName $NEWNAME
					Move-Item -Path $FILEDIR\$NEWNAME -destination $DESTPIC
				}
				else {Move-Item -Path $ITEM -destination $DESTPIC}
			}
		}
	}
	# PREGUNTA SI CREAR CARPETA PICS
	else {
		Write-Host "ERROR EN PATH DE DESTINO: $DESTPIC no existe. Queres crearla?"
		do {
     		Show-Menu
     		$USERINPUT = Read-Host "Elegi"
     		switch ($USERINPUT) {
							's' {MKDIR $DESTPIC
								Clear-Host
                				'Hecho, volve a correr el script para mover el archivo'
								timeout 2
								exit
								}
            				'n' {
                				exit
								}
							}
     						pause
			}
			until ($USERINPUT -eq 'n') 
	}		
	}

# PROCESA VIDEOS
else {
	if (Test-Path -LiteralPath $DESTVID) {
		foreach ($ITEM in $MYFILE) {
			$FILENAMEEXT=split-path -path $ITEM -leaf
			$FILENAME=(Get-ChildItem -path $ITEM).BaseName
			$FILEDIR=split-path -path $ITEM
			if (Test-Path -literalpath $DESTVID\$FILENAMEEXT) {
				$RAND=Get-Random 
				$NEWNAME=($FILENAME + '_' + $RAND + $EXTN)
				Rename-Item -Path $ITEM -NewName $NEWNAME
				Move-Item -Path $FILEDIR\$NEWNAME -destination $DESTVID
				}
			else {Move-Item -Path $ITEM -destination $DESTVID}
			}
		}
	# PREGUNTA SI CREAR CARPETA VIDS
	else {
		Write-Host "ERROR EN PATH DE DESTINO: $DESTVID no existe. Queres crearla?"
		do {
     		Show-Menu
     		$USERINPUT = Read-Host "Elegi"
     		switch ($USERINPUT) {
							's' {MKDIR $DESTVID
								Clear-Host
                				'Hecho, volve a correr el script para mover el archivo'
								timeout 2
								exit
								}
            				'n' {
                				exit
								}
							}
     						pause
			}
			until ($USERINPUT -eq 'n') 



		}	
	}	

Hello,

  1. not sure that the line “foreach-object -process {” is needed because there is a foreach blocn inside.
  2. while a loop foreach ($item in MyFile) when MyFile contains only one file name (i suppose that because you get the extension $extn outside the loop).
  3. no need to split $item to get extension or $filedir. If you start with $fileinfo = get-item -path $item, then $filedir = $fileinfo.directory and $filenameext = $fileinfo.extension

I haven’t test the following script but i have factorized some code :

#DEBUG!!!
#Start-Transcript -path 'E:\Perfil\Desktop\logfile.txt'

function Verify-Path {

	param( $path)

	if (Test-Path -LiteralPath $path) { return $true }

	Write-Host "ERROR EN PATH DE DESTINO: $path no existe. Queres crearla ?"
	do {
		Write-Host "SI: Presiona 'S' para crearla."
		Write-Host "NO: Presiona 'N' para salir."
 		$USERINPUT = Read-Host "Elegi"
 		switch ($USERINPUT) {
			's' { MKDIR $path
				Clear-Host
				'Hecho, volve a correr el script para mover el archivo'
				timeout 2
				return $true
			}
			'n' {
				return $false
			}
		}
	} until ($false)
}

function ProcessOnFile {

	param ( $file, $destpath )

	$item        = Get-ChildItem -path $file
	$FILENAME    = $item.BaseName
	$FILENAMEEXT = $item.extension
	$FILEDIR     = $item.directory
	if ( Test-Path -literalpath "$($destpath)\$($FILENAMEEXT)" ) {
		$RAND    = Get-Random
		$NEWNAME = "$($FILENAME)_$($RAND)$($FILENAMEEXT)"
		Rename-Item -Path $ITEM -NewName $NEWNAME
		Move-Item -Path "$($FILEDIR)\$($NEWNAME)" -destination $destpath
	} else {
		Move-Item -Path $ITEM -destination $destpath
	}
}

#-- variables   --
$MYFILE    = $args
$DESTVID = "E:\Perfil\Desktop\testvid2"
$DESTPIC = "E:\Perfil\Desktop\testpic2"
$TYPE       = '.jpg', '.jpeg', '.png', '.webp', '.bmp', 'tiff'
$EXTN       = [IO.Path]::GetExtension($MYFILE)

if ($EXTN -in $TYPE) {

	# PROCESA IMAGENES
	if ( (Verify-Path -Path $DESTPIC) ) {
		foreach ($file in $MYFILE) {
			ProcessOnFile -file $file -destpath $DESTPIC
		}
	}
} else {

	# PROCESA VIDEOS
	if ( (Verify-Path -Path $DESTVID) ) {
		foreach ($file in $MYFILE) {
			ProcessOnFile -file $file -destpath $DESTVID
		}
	}
}