I have a .txt file containing 2400 separate network paths to directories that i need to 7zip into separate encrypted .7z files.

I believe i can do something like this in power shell however haven't gotten it to work so far

dir | ForEach-Object { & "7za.exe" a $_.BaseName $_.Name }

I've tried the following in a batch file, moving the batch file into the directory when the folders are i want to compress and it works, but i need it to read the directory paths from a txt file containing a list

Any suggestions?

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
  • In Powershell, you can pipe the file contents like: gc paths.txt | % { }. – root Sep 18 '18 at 15:31
  • Sometimes the dumb solution is the best: Copy the .txt file to a .bat file, record a macro in Notepad++ that massages the line and skips to the next, and run it 2400 times. – harrymc Sep 18 '18 at 15:37

So maybe something like this, for a simple pass at this. There are of course other ways of doing this.

# Read the file to parse
$NetworkPaths = Get-Content -Path 'D:\Temp\NetworkPaths.txt'

# Loop through the list and split off path info to name and zip the files in the path provided
ForEach($NetworkPath in $NetworkPaths)
{ Start-Process -FilePath "C:\Program Files\7-Zip\7z.exe" -ArgumentList "a -tzip D:\Temp\$(($NetworkPath.Split('\'))[-1]).zip $NetworkPath\*.* -r -pSECRET" -Wait}

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.