Page 1 of 1
Creating files with subfolders after reading from a list
Posted: 05 Aug 2011 09:44
by vduhan
Hi,
I have a txt file which has a list of filenames wiith directory structure. Example below:
C:\createdocs\1.txt
C:\createdocs\2.txt
C:\createdocs\mydocs\3.txt
C:\createdocs\mysubdocs\4.txt
C:\createdocs\5.txt
I want to create a batch file which will read from this file one by one and will create the file with some dummy value ("this is a test file") at the path provided at each line. If the directory doesn't exist, create the directory as well. Is it possible using the batch scripts?
Re: Creating files with subfolders after reading from a list
Posted: 05 Aug 2011 13:48
by Ed Dyreen
'
What you want is really easy to accomplish.
I am responding because noone else seems to be willing to help.
you will need to use
unfortunately I can't post my own code samples as it's probably above your level. I'm sorry.
There are good tutorials though:
MS-DOS/MSDOS Batch Files: Batch File Tutorial and ... - Allenware
http://www.allenware.com/icsw/icswidx.htm
Re: Creating files with subfolders after reading from a list
Posted: 06 Aug 2011 08:25
by trebor68
Here the simple code to do that.
Code: Select all
@echo off
for /f "tokens=*" %%f in (filelist.txt) do (
echo == %%f ==
if not exist "%%~dpf" md "%%~dpf"
if not exist "%%~dpnxf%%" echo This is a test file.>>"%%~dpnxf"
)
The file "filelist.txt" with the complete drive, path and file name.
In your Example: C:\createdocs\1.txt
If the folder not exist will make the folder.
If not exist the file in the folder will make the file with a file.
If the drive not exist than you can see ERROR messages.
If in the folder name or file name a space " ", not a problem.
Re: Creating files with subfolders after reading from a list
Posted: 06 Aug 2011 08:51
by Ed Dyreen
'
If the folder not exist will make the folder.
The md command is not capable of creating directory trees though, this could become a problem.
His code needs to iterate the md command.
@dennisxy, User is a bot, links to commercial site, verified in google's cache.
Re: Creating files with subfolders after reading from a list
Posted: 07 Aug 2011 23:32
by dennisxy
How many? Word generally saves an original and a backup file of the changes made. It starts with ~$. It gets deleted automatically after you close the document.
HCG Ultra Diet
Re: Creating files with subfolders after reading from a list
Posted: 08 Aug 2011 04:26
by trebor68
In the XP version is this possible with the command "MD" the complete path with one command.
MD drive:\folder1\folder2\folder3
Example:
If that not running please insert the command in the batch file:
Re: Creating files with subfolders after reading from a list
Posted: 08 Aug 2011 14:36
by Ed Dyreen
'
In the XP version is this possible with the command "MD" the complete path with one command.
Unfortunately letting the "MakeDir" command iterate a whole directory structure is more complex than one expects
Do not use as is, reference only:
Code: Select all
@echo off &SetLocal EnableExtensions EnableDelayedExpansion
>&2 pushd "!$Drive!\" &&(
set /a $count = 0
set "$Paths=!$FullPath!"
set "$Paths=!$Paths:*\=!"
set $Paths="!$Paths:\=" "!"
!@forTS! (
!$Paths!
) do (
set /a $count += 1
2>nul pushd "%%~!\" ||(
>&2 md "%%~!" ||!@siError! %=See in errorlevel=%
>&2 pushd "%%~!\" ||!@siError! %=See in errorlevel=%
!@IfErrorBoolExit!
)
)
!@forC! ( 0, 1, !$count! ) do popd
) || !@tError! 18 %=$18.error=insufficient privileges=%
EndLocal