MRG123
MRG123

Reputation: 113

Trouble creating multiple folders from a TXT file in Windows PowerShell or CMD?

I was wondering if there is a way I could create multiple folders from a TXT document in windows PowerShell or CMD? I have a TXT file full of drawing numbers, like 5614-E-1459_SH 1 (except there are about 500 hundred of them). Due to policies at my job I am not allowed to use third party software, so I was wondering if there was a way to do this from the command prompt or Windows PowerShell? I know that mkdir "C:\temp\5614-E-1459_SH 1" will create one of the folders I need. But is there a way to extract the files from a TXT and make it output into folders, without third party software like Text2Folders?

I have gotten this far with the PowerShell script, but as I don't have admin writes at work (where its needed most) I get the Set-ExecutionPolicy error. Is there a work around this?

$Users = Get-Content "C:\Users\usermgx\Desktop\folderDir.txt"
ForEach ($user in $users)
{
$newPath = Join-Path "C:\Users\usermgx\Desktop\Dir" -childpath $user
New-Item $newPath -type directory
}

Upvotes: 3

Views: 6286

Answers (4)

Aaron Jensen
Aaron Jensen

Reputation: 26729

First, you need to update your execution policy so that you can run scripts. You can do it permanently from an administrative PowerShell prompt by running:

Set-ExecutionPolicty RemoteSigned -Scope LocalMachine

If you don't have administrative rights, you can set the execution policy when you call the powershell.exe executable. From CMD:

powershell.exe -ExecutionPolicy RemoteSigned -Command C:\Path\to\your\script.ps1

Finally, you can run your script from the PowerShell ISE. Just open a new untitled document, enter your code, and hit F5, which will execute the code in script pane. I don't believe this is blocked by the execution policy.

Get-Content "C:\Users\usermgx\Desktop\folderDir.txt" |
    ForEach-Object {
        $dirPath = Join-Path "C:\Users\usermgx\Desktop\Dir" $_ 
        New-Item $dirPath -ItemType Directory
    }

Upvotes: 2

foxidrive
foxidrive

Reputation: 41234

This is the same as @zdan but handles certain extra features like long path and filenames and spaces etc in the new foldernames.

@echo off
for /F "delims=" %%a in ('type "C:\Users\usermgx\Desktop\folderDir.txt" ') DO (
    mkdir "C:\Users\usermgx\Desktop\Dir\%%a"
)

Upvotes: 1

zdan
zdan

Reputation: 29450

Fortunately for what you are trying to do this is pretty easy to do with a CMD script and you won;t have to muck with the execution policy:

@echo off
for /F %%u in (C:\Users\usermgx\Desktop\folderDir.txt) DO (
    mkdir "C:\Users\usermgx\Desktop\Dir\"%%u
)

If you want your powershell version to work you must chenge the execution policy as you've noted. But without admin access, you'll have to limit the scope to just yourself, like this:

set-executionpolicy -scope CurrentUser -ExecutionPolicy RemoteSigned    

Upvotes: 1

alroc
alroc

Reputation: 28154

The "workaround" for the "`Set-ExecutionPolicy error'" is to set the execution policy to allow scripts to run. See http://technet.microsoft.com/en-us/library/ee176961.aspx . It's set to the most restrictive by default but any admin worth his salt will set it to something less restrictive based on what the environment requires.

Once you've done that, your script looks solid.

Upvotes: 0

Related Questions