Question

Referencing this posting, what might be the difference between running this on Windows 7 Professional and Server 2008 R2? Server 2008 R2 IS running PS version 2.0 whereas my Windows 7 machine is running 1.0. Would this be enough to cause the following issue (this works correctly until the final trip through the directory, which then throws the error shown below - over and over and over again):

enter image description here

Here is the PS script from that posting:

$dir_files = "C:\path\to\my\files"
$new = "newFile.xml"
$chkFile = "C:\path\to\my\files\newFile.xml"
$fileExists = Test-Path $chkFile

while($true) {
    $xmlFiles = gci $dir_files -include pre_name_*.xml -recurse

    if ($xmlFiles.Length -eq 0) {
        Write-Host "No files in "$dir_Files". Shutting down."
         break
    }

    foreach ($file in $xmlFiles) {
        [string]$strfile = $file
        if (Test-Path $chkFile) {
            Write-Host $chkFile + " file exists! Sleeping 20 seconds ..."
            Start-Sleep -s 20
        } else {        
            if (Test-Path $strfile){        
                Write-Host $chkFile " doesn't exist, renaming next file in array..."
                rename-item -path $file -newname ("$new")
                Start-Sleep -s 10
            } else{
                Write-Host $file " does not exist. Last file processed. Exiting..."
                break
            }        
        }
    }
}

The gist of this is to run through a list of files in a directory and, once a specific type is found, rename it. That renamed file gets processed and deleted and then the next one needs to be renamed until none are left. Then, it will quit/break. The script works fine on the Windows 7 machine. Not so much on the Server 2008 R2 machine. Both are 64bit.

Per request, fleshing request out more fully. Taken from original posting: The original intention was more fully fleshed out in the original posting. Sorry, I tried to keep the original request short but caused more confusion: Task: Directory that will have various .csv files dumped into it. These could be like file1.csv, file2.csv, etc. Problem: Iterate through the files and rename one to a standardized 'newfilename.csv'. This file will then be processed by an external program and get renamed and moved out of the directory. This does not run 24/7. It will be a scheduled task and run until there are no more files in the directory. No sub directories.

UPDATE/FINAL CODE:

$dir_files = "C:\path\to\my\files"
$new = "newFile.xml"
$chkFile = "C:\path\to\my\files\newFile.xml"
#https://stackoverflow.com/questions/20922997/powershell-in-windows-7-versus-server-2008-differences

#changed Include to Filter to improve performance
$xmlFiles = gci $dir_files -include pre_name*.xml -recurse

if (@($xmlFiles).Count -eq 0) {
   Write-Host "No files in '$dir_Files'. Shutting down."
   break
}

foreach ($file in $xmlFiles) {
   #wait until last file is removed (processed by external app)
   while (Test-Path $chkFile){
      Write-Host "$chkFile file exists! Sleeping 20 seconds ..."
      Start-Sleep -s 20
   }
   #continue with the next file
   [string]$strfile = $file
   if (($strFile) -and (Test-Path $strFile)){
      Write-Host "$chkFile doesn't exist, renaming next file in array, sleeping 10 seconds ..."
      Rename-Item -Path $file -newname ("$new")
      Start-Sleep -s 10
   }    
}
Was it helpful?

Solution

Is this what you were after? I've cleaned up/rewritten the code(untested), but as I don't completely understand what you're trying to do it may be wrong.

$dir_files = "C:\path\to\my\files"
$new = "newFile.xml"
$chkFile = "C:\path\to\my\files\newFile.xml"
$fileExists = Test-Path $chkFile

#Changed Include to Filter to improve performance.
$xmlFiles = Get-ChildItem -Path $dir_files -Filter pre_name_*.xml

if (@($xmlFiles).Count -eq 0) {
    Write-Host "No files in '$dir_files'. Shutting down."
    break
}

foreach ($file in $xmlFiles) {
    #Wait until last file is removed(processed by external app)
    while (Test-Path $chkFile) {
        Write-Host "$chkFile file exists! Sleeping 20 seconds ..."
        Start-Sleep -s 20
    }
    #Continue with next file
    Rename-Item -Path $file -NewName $new
    Start-Sleep -s 10
}

To find out what caused the error in your original script, you should run the script using PowerShell ISE and set a breakpoint at if (Test-Path $strfile){ so you can see the value of $strfile each time and detect what happends to it, because as the error says, $strfile suddenly becomes blank (Path property is empty).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top