Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, November 9, 2011 10:14 PM
I need a power shell script to find all the text files in a particular folder and taking each text file, the script should add "99" to the beginning of each text file and save them with another name in the same folder.
Example: File1.txt, File2.txt is in a folder. If I run the script over this folder, this should do the following:
1)Go to File1.txt, open it and add "99" to the beginning of the first line and save it like File1_99.txt in the same folder
2)Go to File2.txt and do the same thing
End;
File1.txt when opened looks like (before): "accounts" "management" .... and so on
File1_99.txt when opened should look like (after): "99" "accounts" "management" .... and so on
Thanks for any help you do!
All replies (12)
Wednesday, November 9, 2011 10:38 PM
Here you go. This will create a copy of each text file in a folder, rename it with _99 and then pre-pend the first line of the file with 99.
$folder = "C:\somefolder"
Get-ChildItem $folder -filter '*.txt' | %{
$info = "99 "
$info += Get-Content $_.FullName
$file = $_.FullName.Replace('.txt','_99.txt')
$info | out-file $file
}
Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP
Engineering Efficiency
@Rich_Prescott
Client System Administration tool
AD User Creation tool
Wednesday, November 9, 2011 10:43 PM
dir *.txt |%{ '99',(gc $_) | out-file ($_.Name -replace '\txt$','_99.txt') }
Thanks,
-Lincoln
Wednesday, November 9, 2011 10:55 PM
I did run this and I see an error.
-> Prompt is not implemented.
And fyi.. I am running this psl using a JAMS scheduler.
And do I need to set the "$info" like an environmental variable?
Wednesday, November 9, 2011 11:33 PM
I did run this and I see an error.
-> Prompt is not implemented.
And fyi.. I am running this psl using a JAMS scheduler.
And do I need to set the "$info" like an environmental variable?
Try running it stand-alone as a .ps1 script. If that works, that is as far as I can take you as I am not familiar with JAMS.Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP
Engineering Efficiency
@Rich_Prescott
Client System Administration tool
AD User Creation tool
Wednesday, November 9, 2011 11:56 PM
(stealing from Lincoln)
dir *.txt |%{ $lines = gc $_ $lines[0] = '99' + $lines[0] $lines | out-file ($_.Name -replace '\.txt$','_99.txt') }
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Come on, this will fail if the text file has no content ( in which case, $lines will be null) and also when the file has only one line ( in which case $line will be a string rather than an array and you cannot index into it. )
Also, out-file ($_.name) means it will write to the file in currentdirectory. You have to use FullName like Prescott did.
Only suggestion I would add over Prescot's answer is to use `-raw` flag with get-content so that the 99 gets added to first line rather than as first line. I think that is what you wanted to do with the $lines[0] = '99' + $lines[0] line.
Wednesday, November 9, 2011 11:58 PM
Yes, I got it. But what if I need to add "99" to the text file? I mean 99 with quotes?
I have done the code like this:
$folder = "C:\somefolder"
Get-ChildItem $folder -filter '*.txt' | %{
$info = ""99""
$info += Get-Content $_.FullName
$file = $_.FullName.Replace('.txt','_99.txt')
$info | out-file $file
}
I see an error:
Unexpected token
Unexpected token '99" "' in expression or statement.
Thursday, November 10, 2011 12:02 AM
Yes, I got it. But what if I need to add "99" to the text file? I mean 99 with quotes?
I have done the code like this:
$folder = "C:\somefolder" Get-ChildItem $folder -filter '*.txt' | %{ $info = ""99"" $info += Get-Content $_.FullName $file = $_.FullName.Replace('.txt','_99.txt') $info | out-file $file }
I see an error:
Unexpected token
Unexpected token '99" "' in expression or statement.
Do
$info = "`"99`""
Thursday, November 10, 2011 12:21 AM
Thanks much Manoj! it worked. But another issue:
If the source file contains special characters like small boxes (unable to put 'em in here), the output file doesnt have those :( (I mean when opened in notepad) But these are actually export files from a database in which only the first word should be added and loaded back into the database. I am worried if the database rejects these output files because of no small boxes like in the source file.
Any suggestions on preserving those too?
Thursday, November 10, 2011 12:25 AM
Above will add a line to the start of the files with the content 99
If your goal is to append "99" to the start of the existing first line, then use below:
dir *.txt |%{
** $content = '"99"' + [io.file]::ReadAllText($_.FullName)**
** [io.file]::WriteAllText( ($_.FullName -replace '\txt$','_99.txt'), $content)**
** }**
Thanks,
-Lincoln
Thursday, November 10, 2011 12:33 AM
OK. You win.[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Thursday, November 10, 2011 1:18 AM
Great!! worked :)
Monday, November 14, 2011 6:46 PM
Above will add a line to the start of the files with the content 99
If your goal is to append "99" to the start of the existing first line, then use below:
dir *.txt |%{
** $content = '"99"' + [io.file]::ReadAllText($_.FullName)**
** [io.file]::WriteAllText( ($_.FullName -replace '\txt$','_99.txt'), $content)**
** }**Thanks,
-Lincoln
I got an error while running the script you've given me:
Exception calling "ReadAllText" with "1" argument(s): Exception of type 'System.OutofMemoryException' was thrown."
At D:\apps\MVPSI\JAMS\Agent... char:51
+ $content ='"Segment99" '+[io.file]::ReadAllText<<<<($_.FullName)
+categoryInfo:NotSpecified:(:) [], MethodInvocationException
+FullyQualifiedErrorId:DotNetMethodException