Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

In Windows 10, how do I automate multiple file creation based on contents of a file (plus an extension I will specify)

I have a text file with the following entries:


31_Section_Intro
32_Activating_Strict_Mode
33_Functions
...
...
49_The_while_Loop
50_Coding_Challenge_4

Open in new window


I am looking for a free utility or built-in functionality in Windows 10 OS (or command line) that will take this list and create matching files in a directory named like this:


31_Section_Intro.js
32_Activating_Strict_Mode.js
33_Functions.js
...
...
49_The_while_Loop.js
50_Coding_Challenge_4.js

Open in new window

In this particular case the file extension is ".js" but it could be ".cs" or ".txt" or whatever.  I just want a fast way to create a bunch of files based on the contents of a file.  The files themselves will be empty.  


The method of doing this can be a small utility or a DOS batch file.


Looking for some recommendations.

ASKER CERTIFIED SOLUTION
Avatar of Ogandos
Ogandos
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Tom,
Here's a script that does what you want using the free, open source AutoHotkey scripting language:

InputFile:="c:\temp\FileList.txt"
OutputFolder:="c:\temp\Output"
Extension:="js"
FileCreateDir,%OutputFolder% ; creates the output folder if it does not exist, and works fine if it does exist
Loop,Read,%InputFile% ; read one line at a time from the input file
  FileAppend,,%OutputFolder%\%A_LoopReadLine%.%Extension% ; FileAppend with no variable creates an empty file
ExitApp ; done

Open in new window

I hope that the descriptive variable names along with the comments in the script provide enough documentation for you to modify it (if needed), but if you have any questions, post them here, and I'll try to assist. I tested the script on W10 with the latest AutoHotkey version...worked perfectly! Btw, it is a "quick-and-dirty" script, in that it does no error checking. If I were writing it for widespread use, I would check the return code (ErrorLevel) coming back from both FileCreateDir and FileAppend, and would also execute each one in a Try-Catch pair.

If you're not familiar with AutoHotkey, my EE article will get you going on it:
AutoHotkey - Getting Started

Regards, Joe
Avatar of oBdA
oBdA

Quick PowerShell one-liner (can be pasted directly into a PS prompt):
Get-Content "C:\Temp\List.txt" | ForEach-Object {New-Item -Path "C:\Target\Directory" -Name "$($_).js"}

Open in new window

Batch (save as Whatever.cmd, can't be pasted into the console):
@echo off
setlocal
set InFile=C:\Temp\List.txt
set TargetDir=C:\Target\Directory
set TargetExt=js

for /f "delims=" %%a in ('type "%InFile%"') do (
	2>"%TargetDir%\%%~a.%TargetExt%" echo Creating '%%~a.%TargetExt%'
)

Open in new window

Avatar of Tom Knowlton

ASKER

RE:  The PowerShell script option:

Okay, if I place a file named "files.txt" into the directory I want to fill with files and I run the PowerShell script, can the script determine where it is being run, and be hard-coded to always use "files.txt", such that I could run the PowerShell script in any directory and it would create the files based on what is inside of "files.txt"?
Hi Tom,

I know you asked this about the PowerShell script, but I figured that I may as well answer it for my AutoHotkey script, too. However, I want to be certain that I understand what you want. I think you are saying that you want the created files to go in the same directory as where the files.txt file is. If that's the case, this modified version of the AutoHotkey script does it:

InputFile:="c:\temp\FileList.txt" ; change as needed
Extension:="js" ; change as needed

SplitPath,InputFile,,OutputFolder ; output folder is where the input file list is
Loop,Read,%InputFile% ; read one line at a time from the input file
  FileAppend,,%OutputFolder%\%A_LoopReadLine%.%Extension% ; FileAppend with no variable creates an empty file
ExitApp ; done

Open in new window

Tested here on W10 with latest AutoHotkey V1 release (1.1.33.10)...worked perfectly! Regards, Joe
In the end I just added the extension to the end of each file name manually (inside of files.txt) and then ran this PowerShell script:

foreach ($line in Get-Content files.txt) {
  #write-host($line)
   New-Item -Path $line -ItemType File
}
Thanks for the update, Tom. I'm glad to hear that my posts helped you to get unstuck. Regards, Joe