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?
Creating files with subfolders after reading from a list
Moderator: DosItHelp
Re: Creating files with subfolders after reading from a list
'
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
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
Code: Select all
for /?
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
Here the simple code to do that.
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.
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
'
His code needs to iterate the md command.
@dennisxy, User is a bot, links to commercial site, verified in google's cache.
The md command is not capable of creating directory trees though, this could become a problem.If the folder not exist will make the folder.
His code needs to iterate the md command.
@dennisxy, User is a bot, links to commercial site, verified in google's cache.
Last edited by Ed Dyreen on 08 Aug 2011 14:34, edited 2 times in total.
Re: Creating files with subfolders after reading from a list
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
HCG Ultra Diet
Re: Creating files with subfolders after reading from a list
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:
MD drive:\folder1\folder2\folder3
Example:
Code: Select all
MD C:\Test\dos\picture\
If that not running please insert the command in the batch file:
Code: Select all
SETLOCAL ENABLEEXTENSIONS
Re: Creating files with subfolders after reading from a list
'
Do not use as is, reference only:
Unfortunately letting the "MakeDir" command iterate a whole directory structure is more complex than one expectsIn the XP version is this possible with the command "MD" the complete path with one command.
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