0

I have files the following files in my path

ENG0100100VOL-815299004001
ENG0100100VOL-815299004002
ENG0100100VOL-815299004003
ENG0100100VOL-815299004004

ENG0100100VOL-815300004001
ENG0100100VOL-815300004002
ENG0100100VOL-815300004003
ENG0100100VOL-815300004004

I want to

  1. create unique folder(s) with the prefix "VOL-" followed by 6 digits identifier and move the respective files of the package to that folder (VOL-81529 and VOL-81530). Ideally its 4 files per folder. NOTE: These values may change overtime and the file numbers are not fixed.

Folder VOL-81529 should contain files

ENG0100100VOL-815299004001
ENG0100100VOL-815299004002
ENG0100100VOL-815299004003
ENG0100100VOL-815299004004

Folder VOL-81530 should contain files

ENG0100100VOL-815300004001
ENG0100100VOL-815300004002
ENG0100100VOL-815300004003
ENG0100100VOL-815300004004
  1. Traverse through the folders and unzip the file with only suffix 002 and 004 (last three digits) of the 12 digits. And remove all the package files from the folder after unzipping

Folder VOL-81529

ENG0100100VOL-815299004002
ENG0100100VOL-815299004004

Folder VOL-81530

ENG0100100VOL-815300004002
ENG0100100VOL-815300004004

I have little knowledge in coding, I have written this and its requires some additional inputs

$files = Get-ChildItem "\My Documents\Path\files" | ForEach-Object{$_.Tostring().substring(14,5)}

$max =$files.count

for($i=0,$i -lt $max, $i++){if ($files[$i] -eq $files[$i+1]) { $value = $files[$i] } else { $value = $files[$i+1]}}

1 Answer 1

2

If you only need the files ending with 002 or 004 we will only treat this ones, right? This code checks for the desired ending of the file and extracts the content of the archives to a subfolder of the target folder. After the extraction it deletes the files. ... and the other files as well. ;-)

$Path = '\My Documents\Path\files'
Get-ChildItem -Path $Path |
ForEach-Object {
    $ID = ($_.BaseName -split '-')[1].Substring(0, 5)
    $TargetPath = Join-Path -Path $_.Directory -ChildPath ('VOL-' + $ID)
    if ( -not (Test-Path -Path $TargetPath)) {
        New-Item -Path $TargetPath -ItemType Directory | Out-Null
    }
    if ($_.BaseName -match '002$|004$') {
        $RenamedItem = Rename-Item -Path $_.FullName -NewName ($_.BaseName + '.zip') -PassThru
        $DestinationPath = Join-Path -Path $TargetPath -ChildPath $_.BaseName
        Expand-Archive -Path $RenamedItem.FullName -DestinationPath $DestinationPath
        Remove-Item -Path $RenamedItem.FullName
    }
    else {
        Remove-Item -Path $_.FullName
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

OK, for step 2 - what are package files? And why did we move files 001 and 003 if we do not need them?
These files are basically zipped files. I can extract them using only 7z or WinRAR. In windows, these are seen as just files without extension. Extracting all these at once will give me 2 zip files and two txt files. Now the purpose of moving all the files is that I don't know that I will receive one file or three or four. But file no 002 and 004 will contain useful info for me. So I want these files to be unzipped using powershell.
OK then. Changed my answer including the code. Try it with test data first please as it's going to delete files. ;-)
Thank you for update. This is working only if the files are in .zip format. Can you suggest me a code for changing or renaming the file extension to ".zip" . I tried to write the code as below Get-ChildItem -Name $Path | Rename-Item -NewName { $_+'.zip' } This errored out as Rename-Item : Cannot rename because item at 'ENG0100100VOL-815300004004' does not exist. At line:1 char:29 + Get-ChildItem -Name $Path | Rename-Item -NewName { $_+'.zip' } +
I tested it on my machine and it worked even with files with no extension at all. ;-) ... anyway .. changed the code again. .... last time now. ;-) :-D ... as usual - test with test data first, please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.