Rename files by removing first x characters and last y characters
April 12, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@echo off
setlocal enabledelayedexpansion

rem set S=0
set /p S="The number of characters to be trimmed from the beginning of the file name: " %=%
rem set X=6
set /p X="The number of characters to be trimmed from the end of the file name: " %=%

set FOLDER_PATH=.
pushd %FOLDER_PATH%
for %%f in (*) do if %%f neq %~nx0 (
    set "filename=%%~nf"
    set "filename=!filename:~%S%,-%X%!"
    ren "%%f" "!filename!%%~xf"
)
popd
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            int y;

            Console.WriteLine("Developed by aripd.com\nThis program renames files in the same file path by removing first and last characters respectively given.\n\n");

            Console.Write("The number of characters to be trimmed from the beginning of the file name: ");
            x = int.Parse(Console.ReadLine());

            Console.Write("The number of characters to be trimmed from the end of the file name: ");
            y = int.Parse(Console.ReadLine());

            Console.WriteLine("Trimming first "+x+" and last " + y + " characters...\n");

            //Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().Location);
            //Console.WriteLine(new Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase).LocalPath);
            //Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().Location);
            //Console.WriteLine(Environment.GetCommandLineArgs()[0]);
            //Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@".");
            //System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\Users\Administrator\Desktop\test\");

            //foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
            foreach (System.IO.FileInfo file in dir.GetFiles("*.pdf"))
                {
                //System.IO.File.Move(file.FullName, file.FullName.ToString().Replace("abc_","");
                //Console.WriteLine("{0}", file.FullName);
                Console.WriteLine("{0}", file.Name);
                //Console.WriteLine("{0}", file.Name.Substring(x).Substring(0, file.Name.Substring(x).Length - y));
                //Console.WriteLine("{0}", System.IO.Path.GetFileNameWithoutExtension(file.Name));
                //Console.WriteLine("{0}", System.IO.Path.GetFileNameWithoutExtension(file.Name).Substring(x).Substring(0, System.IO.Path.GetFileNameWithoutExtension(file.Name).Substring(x).Length - y));
                //Console.WriteLine(file.Extension);
                //Console.WriteLine(file.DirectoryName + System.IO.Path.DirectorySeparatorChar);
                System.IO.File.Move(file.FullName, file.DirectoryName + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(file.Name).Substring(x).Substring(0, System.IO.Path.GetFileNameWithoutExtension(file.Name).Substring(x).Length - y) + file.Extension);
            }

            Console.WriteLine("\n\nCompleted. Press any key to close.");
            Console.ReadLine();

            /*
            Console.WriteLine("Press any key to continue or the Escape (Esc) key to quit: \n");

            do
            {
                cki = Console.ReadKey();
                Console.Write(" --- You pressed ");
                if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
                if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
                if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
                Console.WriteLine(cki.Key.ToString());
            } while (cki.Key != ConsoleKey.Escape);
            */
        }
    }
}