-1

I am new to coding / programming and trying to create a simple robocopy routine that automates the duplication of a template project folder from one location to the main project drive, then renames the "00-000" portion of the folder (which is always at the beginning of the name) and contained subfolders and files based on a user specified variable. Here is what my robocopy routine looks like.

echo off
robocopy "C:\Program Files\Autodesk\ACAD_Config\00-000-NewProject" "T:\Projects" /xd Dfsrprivate /s /e /W:0 /xd DfsrPrivate /r:0 /np

The rename portion is where I get lost. Once the 00-000-NewProject folder has been created on the T:\ drive, it should be time to rename the folder and contained sub-folders and files.

For example,

  1. Routine prompts to set variableA "Enter project number:"
  2. User enters variableA definition, "EP-001"
  3. Routine verifies changes that are about to be made "The Project Number for all folders and files will be renamed to: 'EP-001' is this correct Y/N ?"" (y = confirm changes and on to step 4. n = reenter project number)
  4. This routine replaces any instance of (00-000) contained within the 00-000-NewProject folder with VariableA

Is my idea possible with windows CMD? I would like to avoid installing any 3rd party applications as updates and maintenance can get hairy in a 100 person firm. Any help would be greatly appreciated but further explanation of any suggested code would be most useful in teaching myself how to program. Thank you in advance!

  • What, specifically, do you need assistance with here? Do you need help with how to prompt a user for data? How to do string substitutions? Right now the question is very broad, and effectively: "please do my work for me". If you try to proceed further, or show more of what you've done, and ask specific questions it'll be easier to help you. – Guildencrantz Jan 06 '16 at 20:24
  • @guildencrantz Thanks for the response. I do need help with how to prompt a user for data. I am in fact asking if someone could write out a program like this for me but I also would like someone to explain the components of how it was written so that I can learn for myself through an example of something that I understand. – bryeguy Jan 06 '16 at 21:49
  • welcome to SO, but simply providing requirements and asking for the code is off-topic here. We're happy to answer specific questions, and help you with specific problems, but this is not the place to requisition custom code. – Guildencrantz Jan 06 '16 at 21:54
  • that's fine. maybe you can help me better understand what I am trying to accomplish though. In @Kory's example below, it is my understanding that this will duplicate the primary folder and rename it based on a pre-determined variable. In LISP code, it is possible to query the user and define the variable. How is that possible in Windows programming? – bryeguy Jan 06 '16 at 22:11

2 Answers2

0

I believe this is what you are trying to do, but as @Guildencrantz mentions, you may need to clarify further about your project structure, files, did you mean file names only, directories, etc.

Example batch file

To use, rename d:\test to your t: directory, remove the echo statements so commands execute, etc.

@echo off
setlocal enabledelayedexpansion
set variableA=EP-001
set newfolder=%variableA%-NewProject
echo robocopy "C:\Program Files\Autodesk\ACAD_Config\00-000-NewProject" "D:\test\Projects\%newfolder%" /xd Dfsrprivate 

::: rename all files under T:\Projects\%newfolder% that start with 00-000 prefix with the variableA prefix

for /r D:\test\Projects\%newfolder%\ %%f in (*) do (
::: @echo Found: %%f 
set newname=%%f
call set newname=!newname:00-000=%variableA%!
@echo ren %%f !newname!
)
endlocal

Sample Output:

D:\test>rename1.cmd
robocopy "C:\Program Files\Autodesk\ACAD_Config\00-000-NewProject" "D:\test\Projects\EP-001-NewProject" /xd Dfsrprivate
ren D:\test\Projects\EP-001-NewProject\00-000-one.txt D:\test\Projects\EP-001-NewProject\EP-001-one.txt
ren D:\test\Projects\EP-001-NewProject\00-000-two.txt D:\test\Projects\EP-001-NewProject\EP-001-two.txt
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • Thanks for responding. I am looking to understand a project that would rename every instance of 00-000 within the directory including folders, sub folders, and files. – bryeguy Jan 06 '16 at 21:52
  • Also, if you have time, can you please explain the code your have written out here so that I can better understand your efforts? – bryeguy Jan 06 '16 at 21:52
  • Would it be best to break up the code into multiple programs here? 1 program for duplicating the folder, 1 program for renaming the folders, and 1 program for renaming the files? – bryeguy Jan 06 '16 at 22:09
  • 1
    If you are learning, you will need to do your own research on this site, other searches, etc. to learn all the concepts you find in this and other examples. Your question asked for help for renaming based on a pattern. My answer demonstrates that. For the parts of the batch file you don't understand, you'll need to research and learn in a way that suits your style. – Kory Gill Jan 06 '16 at 22:22
  • @bryeguy, did this answer your question? If you have more, you should [ask another question](http://stackoverflow.com/help/how-to-ask). We are here to help. – Kory Gill Jan 07 '16 at 04:56
0

I see that this is old. It may not matter now, but it was interesting to work out. The difficult spot was renaming the directories from the furthest leaf back to the root.

$project_base = 'C:/src/t/bn'
$template_dir = "$($project_base)/00-000-NewProject"

$project_name_approved = $false
do {
    $pn = Read-Host 'Please enter the new project name'
    $new_project_dir = "$($project_base)/$($pn)-NewProject"

    if (Test-Path -Path $new_project_dir) {
        Write-Host "Project $($pn) already exists. Please choose another project name."
    } else {
        $ap = Read-Host "Rename new project to $($pn)? (Y/N)"
        if ($ap -match '^y') { $project_name_approved = $true }
    }
} until ($project_name_approved)

# Create the new project directory and copy template files
New-Item -ItemType Directory -Path $new_project_dir | Out-Null
Copy-Item  -Path "$($template_dir)\*" -Destination $new_project_dir -Recurse -Exclude DfsrPrivate

# Rename directories first, from the longest leaf in the tree back to the shortest
$dir_names = Get-ChildItem -Recurse -Directory -Path $new_project_dir |
    Sort-Object -Property Length -Descending |
    ForEach-Object {
        $fn = Split-Path $_ -Leaf
        if ($fn -match '^00-000') {
            Rename-Item -Path $_.FullName -NewName "$($fn -replace '^00-000', $pn)"
        }
    }

Get-ChildItem -Path $new_project_dir -File -Recurse |
    ForEach-Object {
        Rename-Item -Path $_.FullName -NewName $($_.Name -replace '00-000', $pn)
    }
lit
  • 14,456
  • 10
  • 65
  • 119