I am working on a script to automate adding users. I want to read from all CSV files in a directory until they are done and then I want to move those csvs to an archive. In the program I am checking a txt file to make sure another instance of the program is not already running. after that I am allowing the code to run or the program to end. When I run the program with 1 or two test files named test.csv and test2.csv I am not having any errors. Once I add random names .csv and try to run these through I am getting the error that import-csv cannot find the file and the path is is telling me the file at it no correct, its from the location of the script.
Here is my code:
$fileDirectory = "c:\test";
$CheckRun = Get-Content c:\test\checkrun.txt
Write-Host $($CheckRun)
if($CheckRun -eq "notrunning") {
Get-ChildItem $fileDirectory -Filter *.csv | Foreach-Object {
$list= Import-Csv $_
$State = "running"
$basename = $_.BaseName
$file = $FileDirectory + "\" + $basename + ".csv"
$ArchiveFile = $fileDirectory + "\archive\" + $basename + "old.csv"
foreach ($entry in $list) {
# will run command here using the entries from the CSVs
write-Host $($entry.EMAIL) $($entry.FIRSTNAME) $($entry.LASTNAME) $($entry.PASSWORD)
}
Move-Item $($file) ${ArchiveFile} -force
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$State > c:\test\Checkrun.txt
}
$State = "notrunning"
$State > c:\test\Checkrun.txt
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
else {
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}