windows 7 - Command or script to paste a file into multiple folders and have the file type overwrite the same of its kind while keeping the original file's name?

06
2014-04
  • Reality

    The title kind of already explains it but I'll be a bit more specific here.

    I'm looking to replace a .jpg file in multiple different folders with a different .jpg file, but I want to be able to keep the original file's name so that I don't have to rename every image to avoid breaking the file path, how would I go about doing this?

    e.g.

    1. Paste the .jpg to 100 folders
    2. .jpg replaces all other .jpg files regardless of file name
    3. The pasted .jpg then renames itself to the original overwritten file's name

    Edit: Forgot to mention, these folders are all children of a single parent, not scattered all over the drive.

  • Answers
  • Peter Hahndorf

    C:\newpic.jpgC:\newpic.jpgThis is what PowerShell is perfect for:

    ls "C:\files" -Recurse -Filter "*.jpg" | ForEach{ cp "C:\newpic.jpg" $_.FullName -WhatIf }
    

    the more verbose version is:

    Get-ChildItem "C:\files" -Recurse -Filter "*.jpg" | ForEach-Object { Copy-Item "C:\newpic.jpg" -Destination $_.FullName -WhatIf }
    

    The first part loops through all jpg files under C:\files including any sub-directories. The second part takes each of those files and overwrites it with your new file.

    The commands above will actually only tell you what they would do without doing it, remove the -WhatIf to actually perform the copy operation.


  • Related Question

    bash - Shell script to rename multiple files from their parent folders name
  • Simon

    I have a file structure like this:

    • 00000010
      • 000000001.file1
      • 000000001.file2
    • 00000020
      • 00000003.file1
      • 00000003.file2
      • 00000003.file3
    • ...

    So there are folders with 8-digit names containing one ore more files with name starting with 8-digit numbers. But theses filenames are – let's say – out of sync. So Now I try to rename them recursively in bash to archive:

    • 00000010
      • 000000010.file1
      • 000000010.file2
    • 00000020
      • 00000020.file1
      • 00000020.file2
      • 00000020.file3
    • ...

    My script does look like:

    #! /bin/bash
    
    find * -maxdepth 1 -name "*" -type d | while read -r dir
    do
            rename 's/$dir\/[0-9]{8}/$dir/' *
    done
    

    But this is not working and gives errors like

    Global symbol "$dir" requires explicit package name at (eval 1) line 1.

    How could I write it to rename the files according to their folder names?

    Thank you for help!


  • Related Answers
  • Simon

    As from another answer I learned, that I have to use

    rename "s/$dir\/[0-9]{8}/$dir\/$dir/" $dir/*
    

    Just in case anyone has the same problem...