Creating files with subfolders after reading from a list

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vduhan
Posts: 1
Joined: 05 Aug 2011 09:34

Creating files with subfolders after reading from a list

#1 Post by vduhan » 05 Aug 2011 09:44

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?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Creating files with subfolders after reading from a list

#2 Post by Ed Dyreen » 05 Aug 2011 13:48

'
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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Creating files with subfolders after reading from a list

#3 Post by trebor68 » 06 Aug 2011 08:25

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Creating files with subfolders after reading from a list

#4 Post by Ed Dyreen » 06 Aug 2011 08:51

'
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.
Last edited by Ed Dyreen on 08 Aug 2011 14:34, edited 2 times in total.

dennisxy
Posts: 1
Joined: 07 Aug 2011 23:10

Re: Creating files with subfolders after reading from a list

#5 Post by dennisxy » 07 Aug 2011 23:32

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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Creating files with subfolders after reading from a list

#6 Post by trebor68 » 08 Aug 2011 04:26

In the XP version is this possible with the command "MD" the complete path with one command.
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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Creating files with subfolders after reading from a list

#7 Post by Ed Dyreen » 08 Aug 2011 14:36

'
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

Post Reply