0

A friend of mine asked me to make a script however I have not worked with power shell. I can't seem to find anything that tells me why this script doesn't run. Any tips are appreciated.

The script is meant to run through all files in a folder and the user enters in information given in the document name. This then is put in the meta data of the file and it moves on to the next file.

#1. opens file or first 10 lines (if plain text)
#1.5 get old name and add it to the new name
#2.gets input from the user as follows
#b)date
#c)classification
#d)fileNum
#e)Org
#f)Lang
#3.fills metadata with some input as params
#4.sets name to YYYYMMDD-classification-fileNum-ORG-NAME-Lang
#5.print out current file name for user to change if needed and confirm

foreach($list in $web.Lists) {

#main folder, change as needed
if ($list.Title -ne "My Documents") {
        continue
    }

 Write-Host $list.Title

    foreach($folder in $list.RootFolder.SubFolders) {

#this is where we get the sub folder, change as needed

        if ($folder.Name -ne "Documents to be changed") {
            continue
        }
Invoke-Item $file

$dateVal = Get-Date -Format "MM/dd/yyyy"
$Name = $file.BaseName
$Class = Read-Host -Prompt 'Input classification'
$fileNum = Read-Host -Prompt 'Input file number'
$Org = Read-Host -Prompt 'Input Orginization'
$Lang = Read-Host -Prompt 'Input Language e for English and f for french'
if($Lang -eq "E"){
$Lang = "EN"
} else if($Lang -eq "e"){
$Lang = "EN"
}else if($Lang -eq "F"){
$Lang = "FR"
}else{
$Lang = "FR"
}
            $file.CheckOut();
            $file.Properties['Classifictation'] = $Class
            $file.Properties['FileNumber'] = $fileNum
            $file.Properties['Orginization'] = $Org
            $file.Properties['Language'] = $Lang
            $file.Properties['Old Name'] = $Name
            $file.Update();
            $file.CheckIn('Updated file');
Write-Host "Current name is: '$dateVal'-'$Class'-'$dateVal'-'$fileNum'-'$Org'-'$Name'-'$Lang' " 
$Correct = Read-Host -Prompt 'enter y if correct, n if incorrect'
if($Correct -eq "y"){
Rename-Item $file -NewName "'$dateVal'-'$Class'-'$dateVal'-'$fileNum'-'$Org'-'$Name'-'$Lang' " 
}else{
$Corrected = Read-Host -Prompt 'Please enter the corrected name'
Rename-Item $file -NewName "'$Corrected'"
}

Get-SmbOpenFile | Where { $_.path -match $file}| Close-SmbOpenFile -Force

}
}
Liam36
  • 9
  • 3
  • 1
    Please read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – mklement0 Aug 21 '20 at 23:25
  • you run this `Invoke-Item $file` but don't seem to define `$File` before you try to use it. where is that $Var defined? – Lee_Dailey Aug 22 '20 at 00:09
  • @Lee_Dailey I thought that just pulls up the current file as it goes through the list. Should I be defining it and where would I put that? – Liam36 Aug 22 '20 at 00:21
  • @LiamStephenson Where does `$web.Lists` come from? Unless it has a value, there's nothing to loop over - which would explain "why this script doesn't run" :) – Mathias R. Jessen Aug 22 '20 at 01:21
  • @LiamStephenson - yes, it WOULD do that ... but you don't have the variable defined _before_ you try to use it. read thru your code and tell us what `$File` contains at that point ... it is not listed in the code you presented. – Lee_Dailey Aug 22 '20 at 13:24
  • @Lee_Dailey I see it contains nothing now, I was told it should get the current file in the list but it seems both ```$list``` and ```$file``` are empty. This is my first power shell script so I'll be honest I barely know what I'm doing. How do I fill ```$list``` with files from a directory and how do I make ```$file``` the current file it loops through – Liam36 Aug 22 '20 at 19:44
  • 1
    start by finding out where `$Web` comes from. then add each line as you get it working. you need to start at the very beginning ... and i have no idea whatsoever as to where on earth you are getting `$Web` since you failed to define it in your code or your text. [*grin*] ///// **_i would toss out the entire pile of [really icky] code you posted and start all over. do ONE thing at a time. get just that tiny thing working - perhaps iterating thru the files and just listing them to the screen. then, when that works, add the next step._** – Lee_Dailey Aug 22 '20 at 20:39
  • I would suggest breaking this down into the steps. First run Invoke-File with a file you know exists, e.g. Invoke-File a.txt - I would expect notepad to open. If you simply execute each command in a console then you'll find it easier to see if it matches what you think it would do. Then you can put it into a script once you're sure it's doing what you think it should. – cyborg Aug 22 '20 at 23:55
  • @cyborg thanks for the help, I'm brand new to this stuff and was using code I was told worked so I'll try working through it slow to figure it out. – Liam36 Aug 23 '20 at 06:43
  • @Lee_Dailey thanks for the tips I'm new to this and was told it all worked and just needed light modification, I'll work through it like you said. – Liam36 Aug 23 '20 at 06:44
  • 1
    @LiamStephenson - you are welcome! glad to kinda-sorta help ... and i wish you the best of good luck! [*grin*] ///// when you get started and get stuck - even if the stuck part seems too simple - _ask for help with that specific thing_. show your code, what worked, what didn't work ... and ask for help. you will almost certainly get neatly to-the-point advice and examples. [*grin*] – Lee_Dailey Aug 23 '20 at 13:35
  • Light modification perhaps for someone familiar with the language, not much help if you're completely new to it. – cyborg Aug 23 '20 at 16:26

0 Answers0